Automating AWS Amplify: Streamlining CI/CD with Shell & Expect Scripts
.png)
Introduction
Automating cloud infrastructure and deployments is a crucial aspect of DevOps. AWS Amplify provides a powerful framework for developing and deploying full-stack applications. However, initializing and managing an Amplify app manually can be time-consuming, especially when integrating it into a CI/CD pipeline like Jenkins.
This blog explores how we automated the Amplify app creation process in headless mode using shell scripting and Expect scripts, eliminating interactive prompts to streamline our pipeline.
Setting Up AWS and Amplify CLI
1. Configure AWS Credentials
Before initializing an Amplify app, configure AWS CLI with your Access Key and Secret Key:
aws configure
2. Install and Configure Amplify CLI
To install Amplify CLI and configure it:
npm install -g @aws-amplify/cli
amplify configure
This will prompt you to create an IAM user and set up authentication.
Automating Amplify App Creation
1. Initialize the Amplify App Using a Script
We created a shell script amplify-init.sh to automate the initialization process.
amplify-init.sh
#!/bin/bash
set -e
IFS='|'
AMPLIFY_NAME=amplifyapp
API_FOLDER_NAME=amplifyapp
BACKEND_ENV_NAME=staging
AWS_PROFILE=default
REGION=us-east-1
AWSCLOUDFORMATIONCONFIG="{\
\"configLevel\":\"project\",\
\"useProfile\":true,\
\"profileName\":\"${AWS_PROFILE}\",\
\"region\":\"${REGION}\"\
}"
AMPLIFY="{\
\"projectName\":\"${AMPLIFY_NAME}\",\
\"envName\":\"${BACKEND_ENV_NAME}\",\
\"defaultEditor\":\"Visual Studio Code\"\
}"
amplify init --amplify $AMPLIFY --providers $AWSCLOUDFORMATIONCONFIG --yes
Run the script:
./amplify-init.sh
2. Automating API and Storage Integration
Since Amplify prompts users for inputs, we used Expect scripts to automate API and storage creation.
add-api-response.exp
#!/usr/bin/expect
spawn ./add-api.sh
expect "? Please select from one of the below mentioned services:\r"
send -- "GraphQL\r"
expect eof
add-storage-response.exp
#!/usr/bin/expect
spawn ./add-storage.sh
expect "? Select from one of the below mentioned services:\r"
send -- "Content\r"
expect eof
These scripts eliminate manual input, making Amplify API and storage additions fully automated.
Automating Schema Updates
One of the biggest challenges was automating schema.graphql updates without manual intervention. The usual approach required engineers to manually upload the file, leading to potential errors.
To solve this, we automated the process with an Amplify Pull script.
amplify-pull.sh
#!/bin/bash
set -e
IFS='|'
AMPLIFY_NAME=amp3
API_FOLDER_NAME=amp3
BACKEND_ENV_NAME=prod
AWS_PROFILE=default
REGION=us-east-1
APP_ID=dzvchzih477u2
AWSCLOUDFORMATIONCONFIG="{\
\"configLevel\":\"project\",\
\"useProfile\":true,\
\"profileName\":\"${AWS_PROFILE}\",\
\"region\":\"${REGION}\"\
}"
AMPLIFY="{\
\"projectName\":\"${AMPLIFY_NAME}\",\
\"appId\":\"${APP_ID}\",\
\"envName\":\"${BACKEND_ENV_NAME}\",\
\"defaultEditor\":\"code\"\
}"
amplify pull --amplify $AMPLIFY --providers $AWSCLOUDFORMATIONCONFIG --yes
This script ensures that the latest schema changes are pulled and updated in the pipeline automatically.
Integrating with Jenkins
Since this automation was integrated with a Jenkins pipeline, we enabled "This project is parameterized" to allow file uploads directly into the workspace.
- Upload the schema.graphql file via Jenkins UI.
- The script pulls the latest changes and updates Amplify automatically.
This method eliminates manual intervention, ensuring consistency in schema updates across multiple environments.
Conclusion
By automating AWS Amplify workflows with shell scripting and Expect scripts, we achieved: Fully automated Amplify app creation
Eliminated manual schema updates
Seamless integration with Jenkins pipelines
Faster deployments with reduced errors
This approach significantly minimized manual effort, ensuring that updates were streamlined and efficient. If you're using Amplify for your projects, automation like this can save countless hours and improve developer productivity.
Have questions or feedback? Drop a comment below!