Friday, August 18, 2023
HomeSoftware EngineeringLearn how to join an API Gateway to Inline Lambda in CloudFormation

Learn how to join an API Gateway to Inline Lambda in CloudFormation


To attach an API Gateway to an inline Lambda perform utilizing CloudFormation, you may comply with these steps:

  1. Outline your API Gateway and Lambda perform sources in your CloudFormation template. Right here’s an instance:
Assets:
  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::Technique
    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/features/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::Perform
    Properties:
      FunctionName: MyLambdaFunction
      Runtime: python3.8
      Handler: index.lambda_handler
      Code:
        ZipFile: |
          def lambda_handler(occasion, context):
              return {
                  'statusCode': 200,
                  'physique': 'Hiya from Lambda!'
              }

Within the above instance, the Lambda perform is outlined inline utilizing the ZipFile property. The code contained in the lambda_handler perform might be personalized 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 sources, you may 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 might be routed to your Lambda perform for processing.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments