Remote Development on AWS: from Cloud9 to VS Code
20 November 2024 - 2 min. read
Alessio Gandini
Cloud-native Development Line Manager
#!/bin/bash apt update && \ apt install snapd && \ snap install aws-cli --classic && \ apt install -y apache2 && \ ufw allow 'Apache' && \ INSTANCE_ID="`wget -q -O - http://instance-data/latest/meta-data/instance-id`" && \ /snap/bin/aws autoscaling complete-lifecycle-action --lifecycle-action-result CONTINUE --instance-id $INSTANCE_ID --lifecycle-hook-name test-hook --auto-scaling-group-name test-asg --region eu-west-1 || \ /snap/bin/aws autoscaling complete-lifecycle-action --lifecycle-action-result ABANDON --instance-id $INSTANCE_ID --lifecycle-hook-name test-hook --auto-scaling-group-name test-asg --region eu-west-1The User Data is going to install Apache Web Server on Ubuntu 18.04. When the installation is completed, it harvests the instance-id and uses it to complete the lifecycle action with CONTINUE result, which tells to the Auto Scaling group to put the instance into InService state.If anything goes wrong, complete-lifecycle-action command will be invoked with ABANDON result, telling the Auto Scaling group to terminate the instance.Take care of --lifecycle-hook-name and --auto-scaling-group-name parameters’ values used in the configuration script; they should correspond, respectively, to the name of the EC2 Auto Scaling Lifecycle Hook and to the name of the EC2 Auto Scaling group that you are going to create later.Firstly, you have to create a new Launch Template, specifying ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-20190722.1 as the source AMI and the configuration script, described above, as User Data field.Then, you can create a new Auto Scaling group based on the just created Launch Template. During the Auto Scaling group configuration, specify one or more subnets in which the EC2 instances will have internet access to. Otherwise, they will not be able to install Apache Web Server.Before moving on, make sure that the Auto Scaling group’s initial DesiredCapacity and min parameters’ values are set to zero; the initial max parameter’s value can be set to one.Move to the Auto Scaling group’s Lifecycle Hooks tab and click the Create Lifecycle Hook button. Configure the lifecycle hook as illustrated in the picture below.To test the lifecycle hook’s behavior, increase the DesiredCapacity parameter’s value by one.A new instance will be added to the Auto Scaling group. Under the Auto Scaling group’s Instances tab, you should see the newly created instance’s Lifecycle column set to Pending:Wait. That means the instance’s lifecycle is paused to the Pending state until complete-lifecycle-action command is invoked from the User Data script.When the Status column changes to InService, log into the new EC2 instance through SSH. To check the status of the Apache Web Server, type sudo systemctl status apache2If the response of this command is something like...