0

I have a SAM app that besides an api-gateway and lambda, also creates an SNS topic and publishes to it.
To publish to the topic I need the topic's ARN which consists of the account ID. Is there a dynamic way (so I do not need to put it in an env var) to get that account ID?
To reiterate:

  1. I have a sam app with lambda and SNS.
  2. I deploy to the cloud, SNS is created in the cloud.
  3. Iinvoke the lambda, Which works, but only if I hard-code the account id where I deployed in step 1.

Is there a way around hard coding or defining an env-var for this?

Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
  • Not 100% sure this is what you're looking for, but CloudFormation templates can use the [AWS::AccountId](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html#cfn-pseudo-param-accountid) pseudo-parameter. – jarmod Sep 23 '21 at 13:19
  • But if I inbvoke it local, the acct id is 1234560109 (or similar). – Itay Moav -Malimovka Sep 23 '21 at 14:18
  • Not sure I understand. The pseudo-parameter represents the AWS account ID of the account in which the stack is being created. This can be used with standard template [string interpolation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-sub.html) features to compose in-account ARNs. ARNs associated with resources created within the template can be retrieved dynamically using [GetAtt](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getatt.html). – jarmod Sep 23 '21 at 14:23
  • ahhh...but I do ```sam local invoke``` – Itay Moav -Malimovka Sep 23 '21 at 20:33
  • 1
    Assuming your Lambda function gets these runtime values, such as SNS ARN, from environment variables, then you could create a local bash script that sets the relevant environment variables, and source that script before using `sam local invoke`. Populate the script with values that are outputs from the CloudFormation stack (retrieving those can also be scripted using awscli). – jarmod Sep 23 '21 at 21:50
  • That might be a direction I should try. tnx – Itay Moav -Malimovka Sep 23 '21 at 22:32
  • 1
    I think this question may be of great help, https://stackoverflow.com/questions/48104665/aws-sam-local-environment-variables – petey Sep 24 '21 at 13:10

0 Answers0