Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 30, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 30, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 30, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 30, 2025

      Does Elden Ring Nightreign have crossplay or cross-platform play?

      May 30, 2025

      Cyberpunk 2077 sequel enters pre-production as Phantom Liberty crosses 10 million copies sold

      May 30, 2025

      EA has canceled yet another game, shuttered its developer, and started more layoffs

      May 30, 2025

      The Witcher 3: Wild Hunt reaches 60 million copies sold as work continues on The Witcher 4

      May 30, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      How Remix is shaking things up

      May 30, 2025
      Recent

      How Remix is shaking things up

      May 30, 2025

      Perficient at Kscope25: Let’s Meet in Texas!

      May 30, 2025

      Salesforce + Informatica: What It Means for Data Cloud and Our Customers

      May 30, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Does Elden Ring Nightreign have crossplay or cross-platform play?

      May 30, 2025
      Recent

      Does Elden Ring Nightreign have crossplay or cross-platform play?

      May 30, 2025

      Cyberpunk 2077 sequel enters pre-production as Phantom Liberty crosses 10 million copies sold

      May 30, 2025

      EA has canceled yet another game, shuttered its developer, and started more layoffs

      May 30, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Securely Interacting with AWS Services Using Boto3 API

    Securely Interacting with AWS Services Using Boto3 API

    January 17, 2025

    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.

    Setting Up Python, Boto3 API, AWS and VS Code Editor

    Python

    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

    VS Code

    I am using VS Code editor tool for developing the boto3 Api code, so we also need to ensure few things in code editor.

    1. 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.

    1

    1. We also need to ensure if python version showing on right bottom bar when writing in python file. this will be available once we setup the python properly in our system.

    2

    Boto3

    Once python and VS Code setup done then we need to install the python boto3 package from command “pip install boto3”.

    • boto3 package will not be recognize and give error during execution until we install it. see in given screenshot where you can see yellow underline under boto3.

    3

    • To run this command in VS Code editor, we can open terminal from Terminal >> New Terminal and run this command there. you can see the installation in below screenshot where few other dependent packages also installed along with boto3 package. later it was also asking me to upgrade pip (python package manager), so I run that command as well.

    4

    • Now we are ready with boto3 api

    AWS

    Configure your AWS credentials using the AWS CLI or by setting environment variables.

    • if we need to use aws cli then we need it install it first.
    • You can use download and installation instructions from here
      • Installing or updating to the latest version of the AWS CLI – AWS Command Line Interface
    • once aws cli installed then we can use “aws configure” command to set the credentials
      • more details, you can find here Configuring settings for the AWS CLI – AWS Command Line Interface

    Securely Managing AWS Credentials

    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.

    1. Environment Variables: Store your AWS credentials in environment variables instead of hardcoding them in your scripts.
    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
    )
    1. IAM Roles: Use IAM roles for EC2 instances to avoid storing credentials on the instance.
    session = boto3.Session()
    s3 = session.resource('s3')

    Different AWS Services Interaction with boto3 API

    Let’s explore how to interact with some common AWS services securely.

    Amazon S3

    Amazon S3 is a widely used storage service. Here’s how to securely interact with S3 using Boto3.

    1. Uploading Files
    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')

    5

    6

    1. Downloading Files
    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)

    7

    Amazon EC2

    Amazon EC2 provides scalable computing capacity. Here’s how to manage EC2 instances securely.

    1. Launching an Instance
    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'
    )

    8

    9

    1. Stopping an Instance
    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()

    10

    11

    For Other Services

    You can go through other services and detailed documentation here Boto3 1.35.91 documentation

    Best Practices for Secure Boto3 Interactions

    1. Use Least Privilege: Ensure that your IAM policies grant the minimum permissions required for your tasks.

    2. Rotate Credentials Regularly: Regularly rotate your AWS credentials to reduce the risk of compromise.

    3. 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.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleEuropean Privacy Group Sues TikTok and AliExpress for Illicit Data Transfers to China
    Next Article Methods for identifying desktop, mobile, or tablet device in the LWC component

    Related Posts

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-5367 – “PHPGurukul Online Shopping Portal SQL Injection Vulnerability”

    May 31, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-5016 – Relevanssi WordPress Stored Cross-Site Scripting Vulnerability

    May 31, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    Dutch Regulator Fines Uber €290 Million for GDPR Violations in Data Transfers to U.S.

    Development

    Development Release: deepin 25 Alpha

    News & Updates

    Are you playing DOOM: The Dark Ages when it launches? — Weekend discussion 💬

    News & Updates

    How to Steer AI Adoption: A CISO Guide

    Development

    Highlights

    CVE-2025-47226 – Grokability Snipe-IT Authorization Bypass

    May 2, 2025

    CVE ID : CVE-2025-47226

    Published : May 2, 2025, 9:15 p.m. | 2 hours, 15 minutes ago

    Description : Grokability Snipe-IT before 8.1.0 has incorrect authorization for accessing asset information.

    Severity: 5.0 | MEDIUM

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    Kodeco Podcast: All the Conferences – Podcast V2, S3 E3 [FREE]

    April 24, 2025

    Why Supply Chain Automation is Necessary for Business Growth?

    April 25, 2024

    AlphaProteo generates novel proteins for biology and health research

    May 27, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.