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.
Step 3: Configure the aws credentials on secrets section as shown image below. Get the aws credentials using the IAM roles or user.
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 }}
- 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.
- Jobs:
- There is one job defined in this workflow named “aws-cli-in-github-actions”.
- This job runs on an Ubuntu latest environment.
- Steps:
- Checkout Code:
- This step uses the
actions/checkout@v2
action to check out the repository’s code.
- This step uses the
- Setup Python:
- This step uses the
actions/setup-python@v2
action to set up Python. - Python version 3.10 is specified.
- This step uses the
- 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.
- Checkout Code:
- Environment Variables:
- AWS credentials (access key ID and secret access key) are provided as environment variables using GitHub Secrets (
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
). - These credentials are used by the AWS CLI to authenticate and authorize actions performed in the AWS environment.
- AWS credentials (access key ID and secret access key) are provided as environment variables using GitHub Secrets (
Step 6: Click on the action tab and you will find the github 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.
Step 8: Here is the detail console output of the action file. The file successfully uploaded into the S3 bucket.
Step 9: Here is the proof the file into S3. For your reference refer the below image.
How to use AWS CLI in GitHub Actions ?
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.