Monday, October 23, 2023
HomeSoftware EngineeringHow you can join an API Gateway to Inline Lambda in CloudFormation

How you can join an API Gateway to Inline Lambda in CloudFormation


To attach an API Gateway to an inline Lambda operate utilizing CloudFormation, you possibly can comply with these steps:

  1. Outline your API Gateway and Lambda operate sources in your CloudFormation template. Right here’s an instance:
Assets:
  MyApiGateway:
    Sort: AWS::ApiGateway::RestApi
    Properties:
      Identify: MyApiGateway

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

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

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

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

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

Within the above instance, the Lambda operate is outlined inline utilizing the ZipFile property. The code contained in the lambda_handler operate may be personalized in keeping with your necessities.

  1. Add the required permission for API Gateway to invoke the Lambda operate:
  MyLambdaPermission:
    Sort: 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:
    Sort: AWS::ApiGateway::Deployment
    Properties:
      RestApiId: !Ref MyApiGateway
  1. Affiliate the deployment with a stage (e.g., “prod”):
  MyApiGatewayStage:
    Sort: AWS::ApiGateway::Stage
    Properties:
      StageName: prod
      RestApiId: !Ref MyApiGateway
      DeploymentId: !Ref MyApiGatewayDeployment
  1. After defining these sources, 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 related to your inline Lambda operate. Requests made to the API Gateway endpoint might be routed to your Lambda operate for processing.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments