This project includes the automation of AWS infrastructure setup using Boto3, which includes provisioning and managing resources such as EC2 instances, S3 buckets, SNS topics, Lambda functions, Application Load Balancers (ALB), and Auto Scaling Groups (ASG).
- AWS Command Line Interface (CLI) configured with AWS region, access key and secret key in the config and credentials files located in the .aws folder
- Python 3.10 and above
- Boto3 library (
pip install boto3) on command line
This function is to initialize the EC2 Instances using linux scripts on Ubuntu 22.04 AMI.
ec2_client = boto3.client('ec2')
# Launch EC2 instance
instance_id = launch_ec2_instance('my-app-instance', user_data_script)This is to initialize a bucket for ASG logging etc.
s3_client = boto3.client('s3')
bucket_name = 'my-app-logs'
def create_bucket(name):
# Assume implementation
create_bucket(bucket_name)SNS topics for different alert types are created, and administrators are subscribed through SMS and email. I used a simple SNS arrangement
import boto3
sns_client = boto3.client('sns')
topic_arn = sns_client.create_topic(Name='HealthAlerts')['TopicArn']
sns_client.subscribe(TopicArn=topic_arn, Protocol='email', Endpoint='admin@example.com')This function is to assess instance health
lambda_client = boto3.client('lambda')
def lambda_health_check_handler(event, context):
# Lambda function code hereAn ALB is deployed to balance traffic between frontend and backend, with access logging enabled and stored in the specified S3 bucket.
import boto3
elbv2_client = boto3.client('elbv2')
def deploy_alb_and_register_instances(frontend_instance_id, backend_instance_id, vpc_id, bucket_name):
# Assume implementation
alb_arn = deploy_alb_and_register_instances(frontend_instance_id, backend_instance_id, 'vpc-xxxx', bucket_name)For automatic scaling of the load
import boto3
asg_client = boto3.client('autoscaling')
def create_asg(name, launch_configuration_name):
# Assume implementation
create_asg('my-app-asg', launch_configuration_name)Things that need to be replaced in the code
- Region Identifier: Replace
eu-west-2with the AWS region in which you are deploying your resources. (Line numbers: Various, including but not limited to lines 8, 14, 20, etc.) - Account ID: Replace
xxxxxxxxxxxxxin ARNs with your AWS account ID. (e.g., Line 13 fortarget_group_arn, Line 14 forsns_topic_arn)
- AMI ID (
ami-xxxxxxxxxxxx): Choose an AMI that is compatible with your application requirements. This can be found in the EC2 Dashboard under "AMIs." (Lines 106, 128) - VPC ID (
vpc-xxxxxxxxxxxxx): The ID of the VPC where you want to deploy your resources. Found in the VPC Dashboard. (Line 84) - Subnet IDs (
subnet-xxxxxxxxxxxxx): The IDs of the subnets for deploying the EC2 instances and ALB. Found in the VPC Dashboard under "Subnets." (Lines 85, 196, 250) - Key Pair Name (
xxxxxxxxxxxxx): The name of the EC2 Key Pair for SSH access to the instances. Found or created in the EC2 Dashboard under "Key Pairs." (Line 86) - Security Group ID (
sg-xxxxxxxxxxxx): The ID of the Security Group to associate with the EC2 instances and ALB. Found in the EC2 Dashboard under "Security Groups." (Lines 104, 195)
- Bucket Name (
xxxxxxxxxxxxx): Choose a globally unique name for the S3 bucket. (Lines 72, 191)
- Target Group ARN: After creating a target group in the ELB service, replace
arn:aws:elasticloadbalancing:eu-west-2:xxxxxxxxxxxx:targetgroup/xxxxxxxxxxxxx/xxxxxxxxxxxxxwith the actual ARN. (Lines 13, 206)
- SNS Topic ARN: After creating an SNS topic, replace
arn:aws:sns:eu-west-2:xxxxxxxxxxxxx:xxxxxxxxxxxxx.fifowith the actual ARN. (Line 14)
- Launch Configuration Names: Provide unique names for the launch configurations for frontend and backend instances. (Lines 232, 239)
- Auto Scaling Group Name: Provide a name for the Auto Scaling Group. (Line 250)
- Phone Numbers (
+91xxxxxxxxxx): Replace with actual phone numbers to subscribe to SMS notifications. (Line 168) - Email Addresses (
example@gmail.com): Replace with actual email addresses to subscribe to email notifications. (Line 173)
Ensure that you have the necessary permissions to create and manage these resources in your AWS account.