DEV Community

Darshil Kansara
Darshil Kansara

Posted on

AWS Amplify Manual Deployment's Automation Using "aws cli"

The reason why AWS Amplify is so popular is because it offers a super easy and fast way to set things up. In just a few clicks and within minutes, you can deploy a static website using a simple zipped file. It's like magic!

But has anyone faced the challenge of automating the process without relying on the Git option? Imagine creating the deployment package yourself and then seamlessly deploying that ZIP file directly onto Amplify.

While manual steps can feel seamless and straightforward, automating those same steps can present quite the opposite experience. The convenience of clicking through a process by hand can quickly become a complex puzzle when attempting to replicate it in an automated fashion.

So let's discuss the best possible solution.

Problem Statement

You've successfully created a zip file through the CI steps of your Azure DevOps Pipeline, and now you're faced with a new challenge: How do you smoothly publish these changes to AWS Amplify?

Solution

Assuming you've successfully generated the deploy-ready artifact ZIP file direct from the root folder content of "eg.dist" during the CI steps and have it stored in Azure Pipelines. And AWS Toolkit for Azure DevOps installed into Azure DevOps Organization.

  • Create Public S3 Bucket as it is required to pass the source URL into upcoming aws-cli command to pull the zip file for deployment. Set specific bucket policy for the get, put and delete the object from S3.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetPutDeleteObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:GetObject",
                "s3:PutObject",
                "s3:DeleteObject"
            ],
            "Resource": "Your ARN as per bucket/*"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode
  • Select AWS Shell Script step and mention AWS Credentials & AWS Region. select path of the Zip file folder into Working Directory in Advance.

  • Here comes the automation part: into AWS Shell Script Step

aws s3 cp zipfilename.zip s3://bucketname/zipfilename.zip

aws amplify start-deployment --app-id "AppId" --branch-name "Hosting Environment Name" --region "Region of Amplify" --source-url "https://bucket.s3.region.amazonaws.com/zipfilename.zip"

aws amplify list-jobs --app-id "AppId" --branch-name "Hosting Environment Name" --region "Region of Amplify" --max-items 1

sleep 10

aws amplify list-jobs --app-id "AppId" --branch-name "Hosting Environment Name" --region "Region" --max-items 1

aws s3 rm s3://bucketname/zipfilename.zip

Enter fullscreen mode Exit fullscreen mode

Let's breakdown the above script.
aws s3 cp command copies the zip file from Azure pipeline running agent to S3 bucket.

aws amplify start-deployment command starts the deployment to AWS Amplify.
Noticed the --source-url. A public url of S3 object comes as a value for this argument. ZIP file refence for deployment is taken from this public url.
and AppId you can find into Amplify's App Setting > App Arn having d**********p.

aws amplify list-jobs can list the jobs and we can see the deployment status of the latest job.

and finally checking the status after few seconds we can run aws s3 rm command to remove the bucker object that we don't require anymore.

So, After completion of above script A successful Auto deployment of Amplify Done.

There might be better solution out there so any better solution or any update into above blog are always welcome. because we all are at always learning phase :)
Happy Independence Day and Happy Learning to you too! If you have any more questions or need assistance in the future, feel free to reach out. Enjoy your tech explorations! 🚀

Top comments (0)