Search code examples
amazon-web-servicesaws-cloudformationamazon-iamaws-cloudformation-custom-resource

Can use environment variables with the sub intrinsic function?


I am using the sub function to define a resource within an aws IAM service.

Resource:
  - !Sub 'arn:aws:s3:::example-${TEST1}-${AWS::REGION}-test'

${TEST1}: it is an environment variable that I have in my java project.

${AWS::REGION}: pseudo parameter

I want to know if !sub is able to read the environment variable and if it can't, is there any way I can do it even if it's not with this function


Solution

  • No, you cannot reference environment variables within cloudformation.

    You'll have to wrap the cloudformation deployment with a script that parses the environment variables and feeds them in as parameters, which can then be referenced in the template.