Authfailure: AWS was not able to validate the provided access credentials

Viewed 885

I am trying to create my Gitlab CI/CD pipeline with AWS. The goal is to Terminate the Existing EC2 Instance, Run a new instance from a template, then Associate an Elastic IP to the new EC2. The runner I'm using is a Docker runner using the python:latest image. When I run my CI/CD pipeline I get

An error occurred (AuthFailure) when calling the DescribeInstances operation: AWS was not able to validate the provided access credentials

My .gitlab-ci.yml is as follows:

stages:
  - build
AWS_Install:
  image: python:latest
  stage: build
  tags:
    - Docker
  script:
    - pip install awscli
    - export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
    - export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
    - export AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION
    - echo "running script :)"
    - OLDEC2=$(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" --query "Reservations[*].Instances[*].[InstanceId]" --output text) 
    - aws ec2 terminate-instances --instance-ids "$OLDEC2"
    - sleep 200.0
    - aws ec2 run-instances --launch-template LaunchTemplateId=[launch-template-id],Version=12
    - sleep 120.0
    - NEWEC2=$(aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" --query "Reservations[*].Instances[*].[InstanceId]" --output text)
    - aws ec2 associate-address --allocation-id [allocation-id] --instance-id "$NEWEC2" --allow-reassociation
What I've checked/tried:
    - AWS credentials: They are correct and valid
    - aws configure: Everything sets correctly (checked using aws configure get)
    - Ensured UNIX line endings were being used
    - Adding a variable section to the YAML file to include environment variables
    - Hardcoding credential values
    - New user on AWS with all necessary credentials
    - Using export to get the environment variables
    - Running everything in one script rather than having a before script
    - Having multiple stages/Jobs
1 Answers

Turns out the solution was to use a public runner on GitLab rather than a customer one.

Related