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 Antworten
2
Akzeptierte Antwort

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
EXPERTE
beantwortet vor 9 Monaten
profile picture
EXPERTE
überprüft vor 2 Monaten
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
EXPERTE
beantwortet vor 9 Monaten
  • Please can you guide me more and elaborate please Thank You.

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen