Search code examples
aws-lambdaaws-amplifyaws-amplify-cli

Amplify get Hosting URL in lambda as environment variable


I need the URL of Cloudfront distribution that I added with amplify cli as an environment variable. Status: enter image description here

I found how template variables are added in file "api-cloudformation-template.json" under my function config. Desired Output variable from "hosting/S3AndCloudFront/template.json" is CloudFrontSecureURL. So I added rows to lambda config file, like so:

 {
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Lambda resource stack creation using Amplify CLI",
    "Parameters": {
        ...
        "hostingS3AndCloudFrontHostingBucketName": { // working example
            "Type": "String",
            "Default": "hostingS3AndCloudFrontHostingBucketName"
        },
        "hostingS3AndCloudFrontCloudFrontSecureURL": { // my example
            "Type": "String",
            "Default": "hostingS3AndCloudFrontCloudFrontSecureURL"
        },
    },
    "Resources": {
        "LambdaFunction": {
            "Type": "AWS::Lambda::Function",
            "Metadata": {
                "aws:asset:path": "./src",
                "aws:asset:property": "Code"
            },
            "Properties": {
                ...
                "Environment": {
                    "Variables": {
                        ...
                        "HOSTING_S3ANDCLOUDFRONT_HOSTINGBUCKETNAME": {
                            "Ref": "hostingS3AndCloudFrontHostingBucketName"
                        },
                        "HOSTING_S3ANDCLOUDFRONT_CLOUDFRONTSECUREURL": {
                            "Ref": "hostingS3AndCloudFrontCloudFrontSecureURL"
                        }
                    }
                },

            }
        }
    ....
    },
    ....
}

I'm getting hostingS3AndCloudFrontCloudFrontSecureURL (default value) in process.env.HOSTING_S3ANDCLOUDFRONT_CLOUDFRONTSECUREURL after publishing function.


Solution

  • Try using the Outputs section of the template along with Fn::ImportValue function documentation HERE

    CloudFront Stack:

    {
       ...
       "Outputs" : {
         "CloudfrontDomainOutput" : {
           "Description" : "The cloudfront domain",
           "Value" : { 
               "Fn::GetAtt": [
                               "hostingS3AndCloudFrontCloudFrontSecureURL",
                               "DomainName"
                              ]
           },
           "Export" : {
             "Name" : {"Fn::Sub": "${AWS::StackName}-hostingS3AndCloudFrontCloudFrontSecureURL" }
           }
        }
    }
    
    

    Lambda Stack

    
    
    {
      ...
       "Environment": {
           "Variables": {
                   "HOSTING_S3ANDCLOUDFRONT_HOSTINGBUCKETNAME": {
                       "Ref": "hostingS3AndCloudFrontHostingBucketName"
                    },
                    "HOSTING_S3ANDCLOUDFRONT_CLOUDFRONTSECUREURL": {
                          "Fn::ImportValue" : {"Fn::Sub" : "${CloudFront_Stack_Name}-hostingS3AndCloudFrontCloudFrontSecureURL"}
                    }
             }
         }
    }