Open In App

How to use AWS CLI in GitHub Actions ?

Last Updated : 30 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Through a command-line interface, Amazon offers a powerful tool called the Amazon Web Services Command Line Interface, or AWS CLI, which makes managing AWS resources easier. The importance of this is that it can simplify the process of utilizing AWS services straight from the terminal, removing the requirement to go through the AWS Management Console. Developers, system administrators, and DevOps engineers, who frequently carry out repeated operations and must automate workflows, will find this efficiency very beneficial. Users may configure S3 buckets, manage IAM users and permissions, provision and manage EC2 instances, and execute a plethora of other tasks with complete control over AWS resources using the AWS CLI. It can easily integrate with other command-line tools thanks to its adaptability and integration features.

Step-by-step use GitHub Action – AWS CLI

Step 1: Create a GitHub repository.

Step 2: Here is the repository called as aws-cli-in-github-actions. For your reference, refer the below image.

Repository

Step 3: Configure the aws credentials on secrets section as shown image below. Get the aws credentials using the IAM roles or user.

AWS credentials

Step 4: Create the S3 bucket.

Step 5: Write the gihub acion file and keep it in the path of .github/workflows I have named it as a blank.yml. Here is the my sample github action file to copy the file from the local to S3.

name: aws cli in github actions

on:
workflow_dispatch:

jobs:
aws-cli-in-github-actions:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install awscli
- run: |
ls -lrth
chmod 777 /home/runner/work/bash-script/bash-script/README.md
ls -lrth /home/runner/work/bash-script/bash-script
aws --version
pwd
aws s3 cp /home/runner/work/bash-script/bash-script/README.md s3://demosamples3 --region us-east-2 --cache-control max-age=0
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
  1. Name and Trigger:
    • The workflow is named “aws cli in github actions”.
    • It is triggered manually through the GitHub web interface using the workflow_dispatch event.
  2. Jobs:
    • There is one job defined in this workflow named “aws-cli-in-github-actions”.
    • This job runs on an Ubuntu latest environment.
  3. Steps:
    • Checkout Code:
      • This step uses the actions/checkout@v2 action to check out the repository’s code.
    • Setup Python:
      • This step uses the actions/setup-python@v2 action to set up Python.
      • Python version 3.10 is specified.
    • Install Dependencies:
      • This step installs the AWS CLI dependencies.
      • It upgrades pip and installs the awscli package using pip.
    • Run AWS CLI Commands:
      • This step runs AWS CLI commands to interact with AWS services.
      • It lists files in the directory, changes permissions of the README.md file, lists files again to verify changes, checks the AWS CLI version, prints the current directory, and finally copies the README.md file to an S3 bucket (demosamples3) with specified region (us-east-2) and cache-control settings.
  4. Environment Variables:
    • AWS credentials (access key ID and secret access key) are provided as environment variables using GitHub Secrets (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY).
    • These credentials are used by the AWS CLI to authenticate and authorize actions performed in the AWS environment.

Step 6: Click on the action tab and you will find the github action file.

Action file

Step 7: Click on the aws cli in github action and trigger the action file. The pipeline successfully executed on the gihub using the aws cli.

Console Output

Step 8: Here is the detail console output of the action file. The file successfully uploaded into the S3 bucket.

Detailed Console Output

Step 9: Here is the proof the file into S3. For your reference refer the below image.

S3 with object

Conclusion

AWS CLI plays a vital role in simplifying the management of AWS resources through a command-line interface. By integrating AWS CLI with GitHub Actions, developers, system administrators, and DevOps engineers can automate workflows and streamline cloud management tasks. The step-by-step guide presented in this article demonstrates how to set up AWS CLI in GitHub Actions, perform various AWS operations, and seamlessly integrate them into existing workflows. With AWS CLI and GitHub Actions, users gain efficiency, flexibility, and control over managing AWS resources, empowering them to work more effectively in the cloud. By following the outlined steps, users can harness the power of AWS CLI within their GitHub workflows, enabling seamless deployment, automation, and management of cloud resources.”

AWS CLI in GitHub Actions – FAQs

How to install AWS CLI in GitHub Actions?

To install AWS CLI in GitHub Actions, use the actions/setup-python action to set up Python, then use pip to install the AWS CLI package.

Is GitHub CLI available in GitHub Actions?

Yes, GitHub CLI (gh) is available in GitHub Actions workflows, enabling users to interact with GitHub directly from their workflows using gh commands. It can be used for tasks like creating issues, managing pull requests, and interacting with repositories.

How do I set up AWS actions on GitHub?

To set up AWS actions on GitHub, install the AWS CLI using GitHub Actions setup steps, and configure AWS credentials securely using GitHub Secrets.

How do I store AWS credentials in GitHub actions?

Store AWS credentials securely as GitHub Secrets, then access them as environment variables in GitHub Actions workflows.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads