AWS Lamdbas use CloudFront distribution domain as environment variable

Viewed 420

I am creating a new stack using various AWS components as well as a few custom functions. My stack defines a AWS::CloudFront::Distribution distribution to be created when the sam deploy command runs. I would like to get the distribution's domain name and use it as an environment variable for two of my functions. Reading the documentation here

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cloudfront-distribution.html

states that one should be able to use the domain name of the distribution by doing this:

!GetAtt <logical_id>.DomainName

Thus is went ahead and did this in one of my functions:

 RenameAsset:
    Type: 'AWS::Serverless::Function'
    Properties:
      CodeUri: 'rename-asset/build/distributions/rename-asset-1.0.0-SNAPSHOT.zip'
      Handler: 'fts.assetiq.renamer.AssetRenamer::handleRequest'
      Role: !GetAtt LambdaExecutionRole.Arn
      Environment:
        Variables:
          PUBLIC_CDN_NAME: !GetAtt AssetIQDistribution.DomainName
      Events:
        RenameAssetEvent:
          Type: Api
          Properties:
            RestApiId: !Ref AssetIQApi
            Path: '/assetiq/assets'
            Method: put

Problem is that I am still unable to retrieve the domain name use it as an env var. I have tried several other alternative (like using !Sub instead of !GetAtt) but nothing seems to work. Can anyone shed some light on this. For reference my distribution configuration is the following:

  CloudFrontOriginAccessIdentity:
    Type: 'AWS::CloudFront::CloudFrontOriginAccessIdentity'
    Properties:
      CloudFrontOriginAccessIdentityConfig:
        Comment: 'Asset-IQ Origin Identity'

  AssetIQDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:
      DistributionConfig:
        Origins:
          - DomainName: !GetAtt S3Bucket.DomainName
            Id: !Sub S3-assetiq-${ApplicationStage}
            S3OriginConfig:
              OriginAccessIdentity:
                Fn::Sub: 'origin-access-identity/cloudfront/${CloudFrontOriginAccessIdentity}'
        Enabled: 'true'
        Comment: "asset iq cloudfront distribution"
        Logging:
          IncludeCookies: 'false'
          Bucket: assetiq-logs.s3.amazonaws.com
          Prefix: assetiq-logs_
        DefaultCacheBehavior:
          Compress: 'true'
          AllowedMethods:
            - GET
            - HEAD
            - OPTIONS
          TargetOriginId: !Sub S3-assetiq-${ApplicationStage}
          ForwardedValues:
            QueryString: 'false'
          ViewerProtocolPolicy: redirect-to-https
        PriceClass: PriceClass_100
        ViewerCertificate:
          CloudFrontDefaultCertificate: 'true'
        IPV6Enabled: false
1 Answers

Closing this question as it the issue has been resolved. Turns out the SAM template configuration was correct but I was referring the system environment variable with a wrong key, resulting in a null returned value.

Correcting that on code level resolved the problem and it seems that referencing the domain name the way I did originally works pretty much fine.

Related