Bootstrapping enables you to script software installation and configuration and execute it while launching the instance. It is way of automating EC2 development using bash script.
So let’s use Bootstrap script while launching our EC2 instance.
Basic Knowledge of Creating Instances.
Choose an Amazon Machine Image (AMI) : Here we are using Amazon Linux 2 AMI (HVM) .
Choose an Instance Type : We have chosen t2.micro, General purpose type.
Configure Instance Details: Now select number of instances to be launched, here we are selecting only 1 instance. You can also request for spot instance, select network , subnet, assign public IP, enable termination protection etc.
Important Fields :
- In IAM role assign it admin access or create Administrator role.
- In Advanced settings, select user data as text and write your script in the given box. Here we are going to write a simple script to make an S3 bucket.
#!/bin/bash aws s3 mb s3://mypuinyabucket
Add Storage : Extra or new volume can also be added to instance. Here we will use only the root volume.
Add tags : It can be done by clicking add tag. Each tag is a simple label consisting of a customer-defined key and an optional value that can make it easier to manage, search for, and filter resources. We will skip this and move to the next.
Configure Security Group : It provides security at the protocol and port access level. Each security group works as much the same way as a firewall and contains a set of rules that filter traffic coming into and out of an EC2 instance.
You can either create a new security group or select an existing group ,here we are choosing existing security group and leave other settings as default and proceed to next step.
Review Instance Launch : Review your instance settings and then click launch.
Create Key pair : When prompted for a key pair, Select Create a new key pair, enter a name for the key pair, and then choose Download Key Pair. This is the only chance for you to save the private key file, so be sure to download it. Save the private key file in a safe place. You’ll need to provide the name of your key pair when you launch an instance and the corresponding private key each time you connect to the instance. Alternatively you can also select existing key pair if you have created earlier.
Don’t select the Proceed without a key pair option. If you launch your instance without a key pair, then you can’t connect to it.
Launch Status : Now you can view the status of the launch. Click on View Instance on the bottom of the page. It takes a short time for an instance to launch. Its initial state is pending, after the instance starts it changes to running and it receives a public DNS name.
Our instances has been successfully launched and it is running, now we need to check whether our bootstrap script worked or not.
Click Services, Click S3 under Storage to check the bucket has been created or not.
So here our bucket has been created successfully, by using this bootstrap script you can do more complex tasks and automate your work.