Search code examples
amazon-web-servicesaws-lambdaaws-cloudformationaws-api-gateway

How to get arn of AWS Api gateway when deploying with cloudformation


I am trying to deploy a function and aws api gateway using cloudformation. In LambdaPermission resource there is a property which is SourceArn and it expects the ARN of the resource that will invoke the function, in this case it will be api gateway. Now ApiGateway resource does not provide the output value of arn. So my question is how we can access it?

here is the resource of Lambda Permission where I need to put the value in sourcearn.

LambdaPermission:
    Type: "AWS::Lambda::Permission"
    Properties:
        Action: "lambda:InvokeFunction"
        FunctionName: !GetAtt LambdaFunction.Arn
        Principal: "apigateway.amazonaws.com"
        SourceArn: "How to get this value"

Solution

  • The format:

    SourceArn:
      Fn::Join:
      - ''
      - - 'arn:'
        - !Ref AWS::Partition
        - ":execute-api:"
        - !Ref AWS::Region
        - ":"
        - !Ref AWS::AccountId
        - ":"
        - !Ref "Logical ID of resource of type AWS::ApiGateway::RestApi"
        - "/"
        - !Ref "Logical ID of resource of type AWS::ApiGateway::Stage"
        - "/GET or POST or other HTTP Methods/your/resource/path/here"
    

    An example:

    SourceArn:
      Fn::Join:
      - ''
      - - 'arn:'
        - !Ref AWS::Partition
        - ":execute-api:"
        - !Ref AWS::Region
        - ":"
        - !Ref AWS::AccountId
        - ":"
        - !Ref ApiGatewayRestApiResource
        - "/"
        - !Ref ApiGatewayStageResource
        - "/GET/example"