Cloudformation Deploy API Gateway Resources based on Lambda Aliases and Gateway Stage

0

Hello,

Please how do i deploy my API gateway Method based on Lambda Aliases and Gateway Stage

Example: i have a Post Method under Resource of user (APIGATEWAY/user/POST) i have 3 Stage in the APIGATEWAY (Dev/Test/Prod) I have 3 lamda Aliases (DevAliases/TestAliases/ProdAliases)

I want deploy the lambda as follows using different aliases for the Lambda

METHODSTAGELAMBDA
APIGATEWAY/user/POSTDevDevAliases
APIGATEWAY/user/POSTTestTestAliases
APIGATEWAY/user/POSTProdProdAliases

I would really need a sample code on how to achieve this with cloud formation

Thank you.

2개 답변
2
수락된 답변

Hi,

You'll find lots of examples for CFN templates with Lambda aliases in this page: https://shisho.dev/dojo/providers/aws/Lambda/aws-lambda-alias/

For example, it points to https://github.com/mcnamarabrian/native-cf-lambda-aliasing/blob/b9b12dcbc4355cc6ed5aa2f2ea46896a769eb83b/template-v0.yml#L47

AWSTemplateFormatVersion: 2010-09-09
Description: Sample CF template that illustrates using Alias + Versions

Resources:
  HelloFunction:
    Type: AWS::Lambda::Function
    Properties:
      Handler: index.handler
      Code:
        ZipFile: |
          def handler(event, context):
            print('Hello world')
            return('Hello world')
      Runtime: python3.6
      Role: !GetAtt HelloFunctionRole.Arn

  HelloFunctionRole:
      Type: AWS::IAM::Role
      Properties:
        AssumeRolePolicyDocument:
          Version: 2012-10-17
          Statement:
            - Effect: Allow
              Principal:
                Service:
                  - lambda.amazonaws.com
              Action:
                - sts:AssumeRole
        Path: /
        Policies:
          - PolicyName: root
            PolicyDocument:
              Version: 2012-10-17
              Statement:
                - Effect: Allow
                  Action:
                    - 'logs:*'
                  Resource: 'arn:aws:logs:*:*:*'

  HelloVersion1:
    Type: AWS::Lambda::Version
    Properties:
      FunctionName: !Ref HelloFunction
      Description: Version 1

  HelloAlias:
    Type: AWS::Lambda::Alias
    Properties: 
      FunctionName: !Ref HelloFunction
      FunctionVersion: !GetAtt HelloVersion1.Version
      Name: PROD

Best,

Didier

profile pictureAWS
전문가
답변함 9달 전
profile picture
전문가
검토됨 2달 전
1

I would think it differently. You could have each environment in a separated aws account (dev, test, prod) so that you can handle the environment in isolation and more securely.

profile picture
전문가
답변함 9달 전
  • Please can you guide me more and elaborate please Thank You.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인