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.

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ