Use custom CF stack as Amplify app backend
Is your feature request related to a problem? Please describe. I want to describe all my amplify infrastructure as code, so it can be easily replicated and deployed elsewhere. The main goal is to have a CloudFormation template describing an Amplify app, several branches and its corresponding domains. I've checked the CloudFormation documentation regarding Amplify, and I've managed to create the template, the stack and its resources successfully.
The problem comes when attaching a backend to the Amplify app. I know it does create a CloudFormation stack for each backend. I would want to have this CF template checked into my code and be able to parameterize it to track infrastructure changes in my Github repo. All nested templates are indeed added to the amplify directory, but not the root stack itself.
I've created a stack for a custom backend myself, but I'm having several troubles trying to get my Amplify app to detect the stack and its resources as a backend environment.
Describe the solution you'd like I would like an easy way to connect an Amplify app to an existing stack for a backend, or clearer documentation if there's already one.
Describe alternatives you've considered I'm trying to "trick" the amplify app, reproducing the same steps it follows when creating a new backend environment, so it uses my custom cloudformation stack as its backend stack. This way I can change the template using Amplify CLI commands after the first deployment is successful.
The deployment is successful but the backend does not show into the Amplify UI.
Additional context The steps I've followed so far are:
- I've created Amplify app and Amplify branch CF templates
App template:
AWSTemplateFormatVersion: 2010-09-09
Description: Stack for Amplify App
Parameters:
AppName:
Description: App name
Type: String
GithubToken:
Type: String
Description: GitHub access token
NoEcho: true
Repository:
Description: Github repository URL for Amplify app
Type: String
Resources:
AmplifyRole:
Type: AWS::IAM::Role
DeletionPolicy: Delete
Properties:
RoleName: amplify-app-role
Description: Amplify app IAM role
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- amplify.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: Amplify
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: "amplify:*"
Resource: "*"
AmplifyApp:
DependsOn:
- AmplifyRole
Type: AWS::Amplify::App
DeletionPolicy: Delete
Properties:
CustomRules:
- Source: "</^[^.]+$|\\.(?!(css|gif|ico|jpg|js|png|txt|svg|woff|ttf|map|json)$)([^.]+$)/>"
Status: "200"
Target: "/index.html"
- Source: "/<*>"
Status: "404"
Target: "/index.html"
Description: Amplify app for React app
EnableBranchAutoDeletion: false
EnvironmentVariables:
- Name: CHOKIDAR_USEPOLLING
Value: "1"
- Name: CI
Value: "1"
- Name: REACT_APP_DEBUG
Value: "false"
- Name: _LIVE_UPDATES
Value: '[{"pkg":"@aws-amplify/cli","type":"npm","version":"latest"}]'
IAMServiceRole: !GetAtt AmplifyRole.Arn
Name: !Ref AppName
OauthToken: !Ref GithubToken
Repository: !Ref Repository
Branch template:
AWSTemplateFormatVersion: 2010-09-09
Description: Stack for Amplify App
Parameters:
AppId:
Description: Amplify app ID
Type: String
BranchName:
Description: Github's repository branch
Type: String
Environment:
Description: Backend environment related to this branch
Type: String
Resources:
AmplifyBranch:
Type: AWS::Amplify::Branch
Properties:
AppId: !Ref AppId
BranchName: !Ref BranchName
Description: Deployment branch for Amplify app
EnableAutoBuild: true
EnablePerformanceMode: false
EnablePullRequestPreview: false
EnvironmentVariables:
- Name: USER_BRANCH
Value: !Ref Environment
- I've created a template for a simple backend.
AWSTemplateFormatVersion: 2010-09-09
Description: Root Stack for AWS Amplify Console
Parameters:
DeploymentBucketName:
Description: Name of the common deployment bucket provided by the parent stack
Type: String
Default: DeploymentBucket
AuthRoleName:
Type: String
Default: AuthRoleName
UnauthRoleName:
Type: String
Default: UnauthRoleName
Resources:
DeploymentBucket:
Type: 'AWS::S3::Bucket'
DeletionPolicy: Retain
Properties:
BucketName: !Ref DeploymentBucketName
AuthRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: !Ref AuthRoleName
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Sid: ''
Effect: Deny
Principal:
Federated: cognito-identity.amazonaws.com
Action: 'sts:AssumeRoleWithWebIdentity'
UnauthRole:
Type: 'AWS::IAM::Role'
Properties:
RoleName: !Ref UnauthRoleName
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Sid: ''
Effect: Deny
Principal:
Federated: cognito-identity.amazonaws.com
Action: 'sts:AssumeRoleWithWebIdentity'
Outputs:
Region:
Description: CloudFormation provider root stack Region
Value: !Ref 'AWS::Region'
Export:
Name: !Sub '${AWS::StackName}-Region'
StackName:
Description: CloudFormation provider root stack ID
Value: !Ref 'AWS::StackName'
Export:
Name: !Sub '${AWS::StackName}-StackName'
StackId:
Description: CloudFormation provider root stack name
Value: !Ref 'AWS::StackId'
Export:
Name: !Sub '${AWS::StackName}-StackId'
DeploymentBucketName:
Description: CloudFormation provider root stack deployment bucket name
Value: !Ref DeploymentBucketName
Export:
Name: !Sub '${AWS::StackName}-DeploymentBucketName'
AuthRoleArn:
Value: !GetAtt
- AuthRole
- Arn
UnauthRoleArn:
Value: !GetAtt
- UnauthRole
- Arn
AuthRoleName:
Value: !Ref AuthRole
UnauthRoleName:
Value: !Ref UnauthRole
- I've created stacks using the previous templates.
- I've created all amplify-related files manually, just as Amplify CLI would have:
team-provider-info.json
{
"dev": {
"awscloudformation": {
"AuthRoleName": "<AUTH ROLE CREATED THROUGH THE CUSTOM STACK>",
"UnauthRoleArn": "<UNAUTH ROLE CREATED THROUGH THE CUSTOM STACK>",
"AuthRoleArn": "<AUTH ROLE CREATED THROUGH THE CUSTOM STACK>",
"Region": "<MY APP AND STACK REGION>",
"DeploymentBucketName": "<BUCKET CREATED THROUGH THE CUSTOM STACK>",
"UnauthRoleName": "<UNAUTH ROLE CREATED THROUGH THE CUSTOM STACK>",
"StackName": "<CUSTOM STACK NAME>",
"StackId": "<CUSTOM STACK id>",
"AmplifyAppId": "<AMPLIFY APP CREATED THROUGH THE CUSTOM STACK>"
},
"categories": {}
}
}
Also the #current-cloud-backend, .config and everything else.
- I've uploaded the necessary files to the deployment bucket, so the
amplify pullcommand works:

- I've created a custom
amplifyPush.shscript that takes theSTACKINFOvariable content from the JSON file on the first deployment:
#!/usr/bin/env bash
set -e
IFS='|'
help_output () {
echo "usage: amplify-push <--environment|-e <name>> <--simple|-s>"
echo " --environment The name of the Amplify environment to use"
echo " --simple Optional simple flag auto-includes stack info from env cache"
exit 1
}
init_env () {
ENV=$1
AMPLIFY=$2
PROVIDERS=$3
CODEGEN=$4
AWSCONFIG=$5
CATEGORIES=$6
echo "# Start initializing Amplify environment: ${ENV}"
if [[ -z ${STACKINFO} ]];
then
STACKINFO="$(amplify env get --json --name ${ENV})"
envCache --set stackInfo ${STACKINFO}
echo "STACKINFO="${STACKINFO}
echo "# Importing Amplify environment: ${ENV} (amplify env import)"
amplify env import --name ${ENV} --config "${STACKINFO}" --awsInfo ${AWSCONFIG} --yes;
echo "# Initializing new Amplify environment: ${ENV} (amplify init)"
[[ -z ${CATEGORIES} ]] && amplify init --amplify ${AMPLIFY} --providers ${PROVIDERS} --codegen ${CODEGEN} --yes || amplify init --amplify ${AMPLIFY} --providers ${PROVIDERS} --codegen ${CODEGEN} --categories ${CATEGORIES} --yes
echo "# Environment ${ENV} details:"
amplify env get --name ${ENV}
else
echo "STACKINFO="${STACKINFO}
echo "# Importing Amplify environment: ${ENV} (amplify env import)"
amplify env import --name ${ENV} --config "${STACKINFO}" --awsInfo ${AWSCONFIG} --yes;
echo "# Initializing existing Amplify environment: ${ENV} (amplify init)"
[[ -z ${CATEGORIES} ]] && amplify init --amplify ${AMPLIFY} --providers ${PROVIDERS} --codegen ${CODEGEN} --yes || amplify init --amplify ${AMPLIFY} --providers ${PROVIDERS} --codegen ${CODEGEN} --categories ${CATEGORIES} --yes
echo "# Environment ${ENV} details:"
amplify env get --name ${ENV}
fi
echo "# Done initializing Amplify environment: ${ENV}"
}
...
- And I've set the amplify build settings to use the previous script
version: 1
applications:
- backend:
phases:
build:
commands:
- chmod +x amplifyPush.sh
- NO_COLOR=1 ./amplifyPush.sh --simple
frontend:
phases:
preBuild:
commands:
- yarn install
build:
commands:
- yarn run build
artifacts:
baseDirectory: build
files:
- '**/*'
cache:
paths:
- node_modules/**/*
- I've attached the AdministratorAccess policy to the Amplify app IAM role (sure there must be a smaller set of permissions that's enough).
Hey @asungomez :wave: since this issue was opened we have added new functionality to export your Amplify app to a CDK stack!
Closing issue, if you feel this functionality does not quite cover your use case please reply back to this thread with the relevant information and we can re-open to investigate further 🙂