Python Boto3 Task
Python Boto3 Task
Just Go to : https://www.jetbrains.com/pycharm/download/?section=windows
response = client.run_instances(
ImageId='ami-050cd642fd83388e4',
InstanceType='t2.micro',
KeyName='ProjectV',
MaxCount=1,
MinCount=1,
)
Write the Python Boto3 code for Ec2 Creation
Now Go to AWS EC2 console & check The Instances you have Given while
Configuration
Code Runs Successfully so , Now Go to Same Instance and check the State It is
Stopped or Not.
Code Runs Successfully so , Now Go to Same Instance and check the State It Is Terminated
or Not
Now Lets Do the Same Things Created , Stop and Delete Instance Using Boto3 on EC2
So First Create One Server For Our Use So We Already Written have a Script to Create EC2
in Pycharm so just Run it
Instance is Created After the code Runned Successfully. I give the name to Instance for
Identity - Pycharm
Now Connect to this Instance using EC2 connect
Connected to Instance Successfully.
response = client.run_instances(
ImageId='ami-050cd642fd83388e4',
InstanceType='t2.micro',
KeyName='ProjectV',
MaxCount=1,
MinCount=1,
)
Copy the Code And Paste in Create.py File but Check Key Pair is Present or not.
Now We Need Python to run this File so Install Python Using Command :
sudo yum install python-pip -y
Now It will show Error Because we had not installed boto3 Dependencies
Now just Go to Instance → go to Actions → now Security → and Just Modify the Role
Now just Select Created Role & Update the role
Now check the EC2 Instance console , see the instance Is Terminated
S3 Bucket Creation using Boto3
Now Lets Create the S3 Bucket using Python Boto3
Now Lets Write Code for the Creation of S3 Bucket in us-east-2 Region
1. import boto3
Explanation: This imports the Boto3 library, which allows you to interact with AWS services like S3,
EC2, and others using Python.
2. s3_client = boto3.client('s3')
Explanation: This creates an S3 client object, allowing you to interact with Amazon S3 (Simple
Storage Service). The 's3' argument tells Boto3 that you're specifically working with the S3 service.
3. bucket_name = "your-unique-bucket-name"
Explanation: This creates a variable bucket_name and assigns it a string value that represents the
name of your S3 bucket. This name must be unique across all AWS accounts.
4. response = s3_client.create_bucket( Bucket='bucket_name',
CreateBucketConfiguration={'LocationConstraint': 'us-west-2'} )
Explanation: This creates an S3 bucket with the name specified in the bucket_name variable. The
CreateBucketConfiguration parameter defines the region where the bucket will be created, in this
case, 'us-west-2'.
response = s3_client.create_bucket(
Bucket='bucket_name',
CreateBucketConfiguration={'LocationConstraint': 'us-west-2'} # Specifying our
region
)
Now Code Has been Written for S3 Bucket Creation , Now run it and check the S3 console
Now run the code but it will ask for event so Create One Test Event
Now Run the Code But It will show some time Error so
Go to Configuration → Go to General Configuration → edit Time Min 1 & Max
15 Minutes (give in this Range only ) → Save
We Use SNS ( simple Notification Service ) So Lets Create SNS Topic First
Now Run the Lambda Function Again And See the Result for Destination Working or Not
Adding File
Now Lets Create One More Lambda Function To Delete the Instance or Terminate the Existing Instance
Write the Code for Terminating the Instance and add Existing Instance Id Also & Deploy The Code
Now Again Go to S3-Lamda Function and Add One More Destination
Select Destination type – Lambda Function & Choose lambda-terminateEC2 which we have Created just