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 Answers
2
Accepted Answer

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
EXPERT
answered 8 months ago
profile picture
EXPERT
reviewed a month ago
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
EXPERT
answered 8 months ago
  • Please can you guide me more and elaborate please Thank You.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions