In today’s cloud-centric world, AWS (Amazon Web Services) stands out as a leading provider of scalable and reliable cloud services. Python’s Boto3 library is a powerful tool that allows developers to interact with AWS services programmatically. However, ensuring secure interactions is crucial to protect sensitive data and maintain the integrity of your applications.
Main objective of this blog is to explain how we can interact with different AWS services in a secure way. In this blog, I explained how we can create a session object from AWS credentials (keys and secret keys) which we are fetching from OS environment variables and use session object to interact with AWS services.
You could ensure if Python installed in your system/server by running “python –version” command. We can run same command in any operating system either that is Windows, Linux/Unix or MacOS. if python not installed, then we need to install it first before moving forward.
You can download and install the python from its official page Download Python | Python.org
I am using VS Code editor tool for developing the boto3 Api code, so we also need to ensure few things in code editor.
We need to install Python extension for Visual Studio Code which integrate and offer support for IntelliSense (Pylance), debugging (Python Debugger), formatting, linting, code navigation, refactoring, variable explorer, test explorer, and many more.
Once python and VS Code setup done then we need to install the python boto3 package from command “pip install boto3”.
Configure your AWS credentials using the AWS CLI or by setting environment variables.
Managing AWS credentials securely is the first step in ensuring secure interactions with AWS services. There is two way we can use to interact with different AWS services.
import os import boto3 aws_access_key = os.getenv('AWS_ACCESS_KEY_ID') aws_secret_key = os.getenv('AWS_SECRET_ACCESS_KEY') session = boto3.Session( aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key )
session = boto3.Session() s3 = session.resource('s3')
Let’s explore how to interact with some common AWS services securely.
Amazon S3 is a widely used storage service. Here’s how to securely interact with S3 using Boto3.
import os import boto3 aws_access_key = os.getenv('aws_access_key_id') aws_secret_key = os.getenv('aws_secret_access_key') session = boto3.Session( aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key ) s3 = session.resource('s3') bucket_name = 'sachinsinghfirstbucket' file_path = 'temp/first.txt' s3.Bucket(bucket_name).upload_file(file_path, 'first.txt')
import os import boto3 aws_access_key = os.getenv('aws_access_key_id') aws_secret_key = os.getenv('aws_secret_access_key') session = boto3.Session( aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key ) s3 = session.resource('s3') bucket_name = 'sachinsinghfirstbucket' file_path = 'temp/first_copy.txt' s3.Bucket(bucket_name).download_file('first.txt', file_path)
Amazon EC2 provides scalable computing capacity. Here’s how to manage EC2 instances securely.
import os import boto3 aws_access_key = os.getenv('aws_access_key_id') aws_secret_key = os.getenv('aws_secret_access_key') session = boto3.Session( aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key ) ec2 = session.resource('ec2') instance = ec2.create_instances( ImageId='ami-07b69f62c1d38b012', MinCount=1, MaxCount=1, InstanceType='t2.micro' )
import os import boto3 aws_access_key = os.getenv('aws_access_key_id') aws_secret_key = os.getenv('aws_secret_access_key') session = boto3.Session( aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key ) instance_id = 'i-00ab4568503979da4' ec2 = session.resource('ec2') ec2.Instance(instance_id).stop()
You can go through other services and detailed documentation here Boto3 1.35.91 documentation
Use Least Privilege: Ensure that your IAM policies grant the minimum permissions required for your tasks.
Rotate Credentials Regularly: Regularly rotate your AWS credentials to reduce the risk of compromise.
Enable Logging and Monitoring: Use AWS CloudTrail and CloudWatch to monitor and log API calls for auditing and troubleshooting.
Interacting with AWS services using Boto3 is powerful and flexible, but security should always be a top priority. By following best practices and leveraging AWS’s security features, you can ensure that your applications remain secure and resilient.
]]>