Methods to join an API Gateway to Inline Lambda in CloudFormation


To attach an API Gateway to an inline Lambda perform utilizing CloudFormation, you possibly can observe these steps:

  1. Outline your API Gateway and Lambda perform assets in your CloudFormation template. Right here’s an instance:
Sources:
  MyApiGateway:
    Kind: AWS::ApiGateway::RestApi
    Properties:
      Title: MyApiGateway

  MyApiGatewayResource:
    Kind: AWS::ApiGateway::Useful resource
    Properties:
      RestApiId: !Ref MyApiGateway
      ParentId: !GetAtt MyApiGateway.RootResourceId
      PathPart: myresource

  MyApiGatewayMethod:
    Kind: AWS::ApiGateway::Methodology
    Properties:
      RestApiId: !Ref MyApiGateway
      ResourceId: !Ref MyApiGatewayResource
      HttpMethod: GET
      AuthorizationType: NONE
      Integration:
        Kind: AWS_PROXY
        IntegrationHttpMethod: POST
        Uri: !Sub "arn:aws:apigateway:${AWS::Area}:lambda:path/2015-03-31/capabilities/arn:aws:lambda:${AWS::Area}:${AWS::AccountId}:perform:MyLambdaFunction/invocations"

  MyApiGatewayDeployment:
    Kind: AWS::ApiGateway::Deployment
    Properties:
      RestApiId: !Ref MyApiGateway

  MyApiGatewayStage:
    Kind: AWS::ApiGateway::Stage
    Properties:
      StageName: prod
      RestApiId: !Ref MyApiGateway
      DeploymentId: !Ref MyApiGatewayDeployment

  MyLambdaFunction:
    Kind: AWS::Lambda::Operate
    Properties:
      FunctionName: MyLambdaFunction
      Runtime: python3.8
      Handler: index.lambda_handler
      Code:
        ZipFile: |
          def lambda_handler(occasion, context):
              return {
                  'statusCode': 200,
                  'physique': 'Howdy from Lambda!'
              }

Within the above instance, the Lambda perform is outlined inline utilizing the ZipFile property. The code contained in the lambda_handler perform could be custom-made in line with your necessities.

  1. Add the mandatory permission for API Gateway to invoke the Lambda perform:
  MyLambdaPermission:
    Kind: AWS::Lambda::Permission
    Properties:
      FunctionName: !GetAtt MyLambdaFunction.Arn
      Motion: lambda:InvokeFunction
      Principal: apigateway.amazonaws.com
      SourceArn: !Sub "arn:aws:execute-api:${AWS::Area}:${AWS::AccountId}:${MyApiGateway}/*/*/*"
  1. Deploy your API Gateway:
  MyApiGatewayDeployment:
    Kind: AWS::ApiGateway::Deployment
    Properties:
      RestApiId: !Ref MyApiGateway
  1. Affiliate the deployment with a stage (e.g., “prod”):
  MyApiGatewayStage:
    Kind: AWS::ApiGateway::Stage
    Properties:
      StageName: prod
      RestApiId: !Ref MyApiGateway
      DeploymentId: !Ref MyApiGatewayDeployment
  1. After defining these assets, you possibly can deploy your CloudFormation stack utilizing the AWS CloudFormation service or the AWS CLI.

These steps will create an API Gateway that’s linked to your inline Lambda perform. Requests made to the API Gateway endpoint shall be routed to your Lambda perform for processing.

Leave a Reply

Your email address will not be published. Required fields are marked *