Use other CloudFormation stack outputs as env variables in my ECS tasks
Amplify CLI Version
12.7.1
Question
Hi all!
I've created an API as a serverless container in Fargate using the Amplify CLI. All is good and works. Then I want to set up a DocumentDB for data storage (I need Mongo compatibility, cannot do the default DynamoDB). I've created a custom resource with the stack that creates my cluster, my instances, and a secret in the Secrets Manager with the credentials. The stack outputs are the Cluster endpoint, Cluster port, and Secret ARN.
I would like to inject these 3 values into my Fargate container as environment variables. Here's what I've tried so far:
- Adding them as parameters to the API CF stack: not sure if it's possible. Can't do it through the CLI using the "Access other resources" option, and do not want to do it through the team-provider file because that would mean hardcoding the values, and that's not what I want.
- Adding the Cluster, Instance, and Secret to the API stack as extra resources, so I can reference them directly. They get deleted every time I amplify push (I assume the stack gets regenerated from the docker-compose and overrides any changes I make).
Is there anything else I can try?
Edit: I've managed to inject the parameters into the stack by adding them to the backend-config.json and to the amplify-meta.json, into the dependsOn array, like this:
"MyAPI": {
"apiType": "REST",
"build": true,
"dependsOn": [
{
"attributes": [
"ClusterName",
"VpcId",
"VpcCidrBlock",
"SubnetIds",
"VpcLinkId",
"CloudMapNamespaceId"
],
"category": "",
"resourceName": "NetworkStack"
},
{
"attributes": [
"ClusterEndpoint",
"ClusterPort",
"PasswordSecretArn"
],
"category": "",
"resourceName": "customDatabase"
}
],
"deploymentMechanism": "FULLY_MANAGED",
"iamAccessUnavailable": true,
"imageSource": {
"type": "CUSTOM"
},
"providerPlugin": "awscloudformation",
"resourceName": "graphql",
"restrictAccess": false,
"service": "ElasticContainer",
"skipHashing": false
}
Now I have them in the CF stack:
"customDatabaseClusterEndpoint": {
"Type": "String"
},
"customDatabaseClusterPort": {
"Type": "String"
},
"customDatabasePasswordSecretArn": {
"Type": "String"
},
But every time I try to reference them from the container definition:
"ContainerDefinitions": [
{
"Command": [],
"EntryPoint": [],
"Environment": [
{
"Name": "DB_ENDPOINT",
"Value": {
"Ref": "customDatabaseClusterEndpoint"
}
}
],
It gets overriden by the amplify push command
Hey @asungomez, thank you for reaching out. The issue appears to be similar to feature request https://github.com/aws-amplify/amplify-cli/issues/7049. Utilizing custom resource outputs into a different resource such as API is currently not supported. Closing the issue as duplicate, please feel free in adding a thumbs up to the feature request.
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.