Faisal Shani
Faisal Shani

Reputation: 810

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"

Upvotes: 5

Views: 3002

Answers (2)

Pedro Hoehl Carvalho
Pedro Hoehl Carvalho

Reputation: 2433

If anyone is looking for an example of the more compact !Sub syntax @henry-woody suggested, here it is:

SourceArn: !Sub "arn:${AWS::Partition}:execute-api:${AWS::Region}:${AWS::AccountId}:${RestApi}/*/GET/"

Upvotes: 0

Kaustubh Khavnekar
Kaustubh Khavnekar

Reputation: 2923

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"

Upvotes: 7

Related Questions