AWS offers a lot of different SDKs to configure resources in AWS. This means that you can use your favorite language to code against AWS API like Java Ruby, C++, etc. In my case I decided to use Python3 because of the Boto3 library.
I decided to use Boto3 because it provides a low level API to connect to AWS resources. Also the library is pretty well document and you can find the reference documents here. For this exercise I also decided to write the code in Python3, the only downfall of this decision is that I would not be able to utilize AWS Lambda functions because it only supports Python2.7
Why you may need to attach a second interface to a EC2 instance ?
- Secure network connectivity in a n-tie design like a DMZ zone
- To have public and external interface
- To segregate network traffic
- For monitoring traffic
Steps you need to take before running the code
- Install the AWS cli tools
- Configure authentication to your AWS account using the AWS cli tools
- install Python3
- Know what security group needs to attached to
- Import the the boto3 library using pip
- Every AMI has an ownerID. Make sure you add that to the code so you affect AMI that are not supposed to be modify.
- Make sure you have subnets available on the VPC and Availability Zone.
- Make sure to target correct instances. on the following code I target all running instances.
- Make sure you know the security group that needs to be attached to second ENI