Sunday 10 May 2020

Deploying Infrastructure to AWS LIghtsail Using Azure DevOps – Part 1 - Writing IaC Script with bash

AWS Lightsail is easy to use cloud platform services by Amazon Web Services. You can use aws command line capabilities with Azure DevOps to deploy the required infrastructure on AWS Lightsail. Let’s look at step by step to see how we can get AWS Lightsail deployments via Azure DevOps.

First step is to write an infrastructure as code script using AWS CLI. You can either use bash or PowerShell to write the script. Let’s look at a sample script. The first step of the script below is taking the required arguments. We need below arguments as minimum to create an Lightsail instance.
· Instance name: Name of the Lightsail instance to create.
· Availability zone: The region to create the instance.
· Blue print id: Type of instance to setup such as Windows or Ubuntu 16.04 etc.
You can use AWS Lightsail get-blueprints to retrieve the available list of blueprints and get the required blueprint id using the AWS CLI.
· Bundle id: The pricing tier identification.
You can use AWS Lightsail get-bundles to retrieve the available list of bundles and get the required bundle id using the AWS CLI.

The next step is to check the availability of an AWS Lightsail instance by the name we want to create it. When the instance is not available an error occurs with aws lightsail get-instance command. To hide the error 2>/dev/null is used in below script. +e make sure with the error script still continues execution. -e makes if there is an error the script to halt the execution.

The return value to get instance becomes null if the instance is not available. If it is null can be checked with -z. If the instance by the specified name is not available then it will be created with the provided name, availability zone, type and the specified size. Once the instance is created, it will take sometime to get fully provisioned and come to running state. The script keeps on checking until the state of the Lightsail instance is running. If the instance is already existing and in state of the instance is printed by the script. The full script is available here.

In the next post, lets see how we can create a service connection for AWS in Azure DevOps as the next step of getting the AWS Lightsail infrastructure creation using Azure DevOps.

No comments:

Popular Posts