aws-amplify / amplify-cli

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development.
Apache License 2.0
2.81k stars 819 forks source link

Adding stack dependency from hosting to function #13490

Open ChapterSevenSeeds opened 9 months ago

ChapterSevenSeeds commented 9 months ago

Amplify CLI Version

12.10.0

Question

I am trying to use CloudFormation to automate the deployment of Lambda@Edge with a CloudFront distribution. I have a Lambda function which I created with amplify add function. I also added S3 and CloudFront hosting with amplify add hosting. I added versioning to my function template, and I am outputting the qualified ARN:

"Version": {
      "Type": "AWS::Lambda::Version",
      "Properties": {
        "FunctionName": {
          "Ref": "LambdaFunction"
        }
      }
    }
// ...
"Outputs": {
  "QualifiedARN": {
      "Value": {
        "Ref": "Version"
      }
   }
}

However, in order for the CloudFront template to be able to use this output, I need to change the stack deployment order so that the Lambda function gets deployed before the CloudFront distribution. And, in order to do this, I figured the easiest way would be to use this new QualifiedARN output as a parameter in the hosting template. But, in order to add this parameter, I need to modify the root stack template after it is built but before it is uploaded to CloudFormation. I explored adding a project-wide override, but I don't think that will let me do what I need it to. I did also try using a file system watcher to wait for the root template build output to be overwritten, at which point a script would go and add the new parameter, but that didn't seem to work either.

In essence, inside the root stack template, I need my hosting resource to look like this:

 "hostingS3AndCloudFront": {
      "Type": "AWS::CloudFormation::Stack",
      "Properties": {
        "TemplateURL": "should I redact this?",
        "Parameters": {
          "bucketName": "project-20231217154634-hostingbucket",
          "env": "dev",
          "resolverQualifiedArn": {
            "Fn::GetAtt": [
              "functionmylambda",
              "Outputs.QualifiedARN"
            ]
          }
        }
      }
    }

Is this something that can be done?

ykethan commented 9 months ago

Hey @ChapterSevenSeeds, thank you for reaching. This is currently not supported on Amplify CLI and a feature request is currently open here: https://github.com/aws-amplify/amplify-cli/issues/4939 While the file watcher solution suggested could be a viable solution but it can be inconsistent and unreliable in behaviour. marking this as feature request.