<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Edmund Kwok</title>
    <description>The latest articles on DEV Community by Edmund Kwok (@edmundkwok).</description>
    <link>https://dev.to/edmundkwok</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1031753%2Fd57812bf-3951-4ebd-a024-0cf974e2e109.jpeg</url>
      <title>DEV Community: Edmund Kwok</title>
      <link>https://dev.to/edmundkwok</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/edmundkwok"/>
    <language>en</language>
    <item>
      <title>5 ways for GitLab CI runners to get AWS credentials</title>
      <dc:creator>Edmund Kwok</dc:creator>
      <pubDate>Wed, 15 Mar 2023 15:53:21 +0000</pubDate>
      <link>https://dev.to/edmundkwok/5-ways-for-gitlab-ci-runners-to-get-aws-credentials-11hp</link>
      <guid>https://dev.to/edmundkwok/5-ways-for-gitlab-ci-runners-to-get-aws-credentials-11hp</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What do we mean by "AWS credentials"&lt;/li&gt;
&lt;li&gt;Option 1: GitLab CI Variables&lt;/li&gt;
&lt;li&gt;Option 2: EC2 Instance Profile&lt;/li&gt;
&lt;li&gt;Option 3: EKS IAM Roles for service accounts (aka IRSA)&lt;/li&gt;
&lt;li&gt;Option 4: IAM OIDC identity provider integration with GitLab&lt;/li&gt;
&lt;li&gt;Option 5: HashiCorp Vault&lt;/li&gt;
&lt;li&gt;Which option is the best for me?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Congratulation! You are well on your way to automating pipelines and running them in the cloud. You have settled on using GitLab CI and are now faced with the daunting task of passing AWS credentials to your runners... &lt;em&gt;securely&lt;/em&gt; of course.&lt;/p&gt;

&lt;p&gt;As with everything in tech, there is more than one way to do it. But which one will you choose?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9co4tw7npxm8yvcpyrnr.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9co4tw7npxm8yvcpyrnr.gif" alt="John Legend pressing the button on his chain in The Voice"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Digging deeper, how will you choose and more importantly, why will you choose it?&lt;/p&gt;

&lt;p&gt;And that, my friend, is what we will figure out together in this article (just high level for now, we can dig into the hands-on details for each option in follow-up articles). I'll share my approach to making a choice and hopefully it will be helpful to you too.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  But first, what do we mean by "AWS credentials"?
&lt;/h2&gt;

&lt;p&gt;You have a pipeline.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Froe8qq0lh2ab3w1s3y5i.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Froe8qq0lh2ab3w1s3y5i.gif" alt="A pack of dogs doing stuff one after another, like a pipeline of sorts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Your &lt;del&gt;doggos&lt;/del&gt; CI runners are doing something with AWS - maybe through &lt;code&gt;aws-cli&lt;/code&gt; or AWS SDK or some other library that uses AWS SDK.&lt;/p&gt;

&lt;p&gt;These requests to AWS will need to be signed by a valid AWS credential, more specifically, an AWS access key (&lt;em&gt;authentication&lt;/em&gt;). The access key is tied to one or more permissions that allow you to do stuff in AWS (&lt;em&gt;authorization&lt;/em&gt;). &lt;/p&gt;

&lt;p&gt;For the sake of our discussion here, let's agree that we will be using &lt;code&gt;aws-cli&lt;/code&gt; in our pipeline, and the script will be uploading an object to our S3 Bucket, requiring the &lt;code&gt;s3:PutObject&lt;/code&gt; permission:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsa7szn7d259q4c58xmjp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsa7szn7d259q4c58xmjp.png" alt="Architecture diagram of how the pipeline will perform the  raw `aws-cli` endraw  command"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are three options to pass the access keys to &lt;code&gt;aws-cli&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html" rel="noopener noreferrer"&gt;AWS credentials file&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html" rel="noopener noreferrer"&gt;Environment variables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-options.html" rel="noopener noreferrer"&gt;&lt;code&gt;aws-cli&lt;/code&gt; arguments&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I will leave it to you to decide what's best for your use case.&lt;/p&gt;

&lt;p&gt;Now how do you get an access key? There are a few ways, but the two most common ones are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Access key created for an IAM user &lt;/li&gt;
&lt;li&gt;Access key given after assuming an IAM role (i.e. &lt;code&gt;AssumeRole&lt;/code&gt;, &lt;code&gt;AssumeRoleWithSAML&lt;/code&gt;, &lt;code&gt;AssumeRoleWithWebIdentity&lt;/code&gt;, but we'll mostly reference &lt;code&gt;AssumeRole&lt;/code&gt; here)&lt;/li&gt;
&lt;/ol&gt;




&lt;blockquote&gt;
&lt;h3&gt;
  
  
  Pro Tip to tell the difference
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;1&lt;/code&gt; IAM user access key starts with &lt;code&gt;AKIA&lt;/code&gt;, is long-lived, "static"&lt;br&gt;
&lt;code&gt;2&lt;/code&gt; &lt;code&gt;AssumeRole*&lt;/code&gt; access key starts with &lt;code&gt;ASIA&lt;/code&gt;, is temporary, "dynamic"&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Which is better though? Security security security, means I can't recommend &lt;code&gt;2&lt;/code&gt; enough. Temporary short-lived credentials are preferred as they will expire on their own. If (When?) the credentials are leaked, hopefully they will be expired by then, limiting / eliminating any damage.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgwlypcd855kb0tqnnb29.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgwlypcd855kb0tqnnb29.gif" alt="Bart Simpson accepting what looks like a ticket, and exclaiming "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But, with great features, comes greater effort to get it done properly. So &lt;code&gt;1&lt;/code&gt; may be an option depending on your use case and security hygiene. Fret not, we will discuss options for both &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;2&lt;/code&gt; and you can pick your poison.&lt;/p&gt;

&lt;p&gt;If you got this far, we will &lt;em&gt;assume&lt;/em&gt; that you have an IAM role with the necessary permissions that your CI runners need, which is ready to be &lt;code&gt;AssumeRole&lt;/code&gt;-ed (see what I did there? 😉).&lt;/p&gt;

&lt;h2&gt;
  
  
  Now bring on the options!
&lt;/h2&gt;

&lt;p&gt;The options here are a mix of difficulty, security tradeoffs, and runners type (shared or self-hosted). For self-hosted runners, there are prerequisites as well - using AWS EC2 instance or AWS EKS.&lt;/p&gt;

&lt;p&gt;Ready or not, presenting to you, the first option.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Option 1: GitLab CI Variables
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Access key type&lt;/strong&gt; - IAM user, &lt;code&gt;AssumeRole&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnp83ekp6q7ejrcmlq50j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnp83ekp6q7ejrcmlq50j.png" alt="Architecture diagram for using GitLab CI Variables"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the easiest way to get started without any dependency but probably the least secure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.gitlab.com/ee/ci/variables/#define-a-cicd-variable-in-the-ui" rel="noopener noreferrer"&gt;Variables can be added via the GitLab UI&lt;/a&gt;, at the project level, group level, and instance level - you literally copy the access key and paste that into GitLab CI Variables UI. The access key will then be available to your pipeline script as environment variables or a file, depending on your use case.&lt;/p&gt;

&lt;p&gt;It is also possible to go with an &lt;code&gt;AssumeRole&lt;/code&gt; approach by adding a few steps in your script to assume the intended role. The access keys in the GitLab CI Variables should have the necessary permissions to &lt;code&gt;AssumeRole&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  ⚠️ Important PSA!
&lt;/h3&gt;

&lt;p&gt;Do not put any access key and secret in the &lt;code&gt;.gitlab-ci.yml&lt;/code&gt; file in any shape or form. There has been &lt;a href="https://www.scmagazine.com/news/cloud-security/leaked-shiba-inu-aws-credential-exposed-on-public-github-repository" rel="noopener noreferrer"&gt;many&lt;/a&gt; &lt;a href="https://www.techtarget.com/searchsecurity/news/252525808/Former-Uber-CSO-Joe-Sullivan-found-guilty-in-breach-cover-up" rel="noopener noreferrer"&gt;reported&lt;/a&gt; &lt;a href="https://www.bleepingcomputer.com/news/security/toyota-discloses-data-leak-after-access-key-exposed-on-github/" rel="noopener noreferrer"&gt;instances&lt;/a&gt; where secrets in Git repos are leaked.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why this
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Easiest to implement, no additional scripts to manage (if using the IAM user access key approach).&lt;/li&gt;
&lt;li&gt;Runners can be hosted anywhere, even shared runners - the other approaches require it to be EC2 instances or Kubernetes Pods.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why not this
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The keys are in "plain text" and can be read by anyone with relevant access to the repo.&lt;/li&gt;
&lt;li&gt;If Your / team member's GitLab account is compromised, the access keys will be as well.&lt;/li&gt;
&lt;li&gt;GitLab itself could be compromised...&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Big caveats before you adopt this
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Make sure the privilege granted to the access key adheres to the least privilege approach. It may be tempting to just assign the &lt;code&gt;AdministratorAccess&lt;/code&gt; managed policy for convenience but think about the major inconvenience if the keys are compromised.&lt;/li&gt;
&lt;li&gt;Have a key rotation policy, revoking the old access keys and generating new ones periodically.&lt;/li&gt;
&lt;li&gt;Avoid this on production AWS accounts as much as possible. Or if you like living on the edge, and can be certain that the possible leak of production credentials won't be the end of the world for you.&lt;/li&gt;
&lt;li&gt;Have an up-to-date "compromised credentials" playbook that can be triggered as soon as a compromise is suspected.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Option 2: EC2 Instance Profile
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Access key type&lt;/strong&gt; - &lt;code&gt;AssumeRole&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdy6wxyjibtj2msml8v21.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdy6wxyjibtj2msml8v21.png" alt="Architecture diagram for using EC2 Instance Profile"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the second easiest way to get access keys into your pipeline if you are already using EC2 instances for your self-managed runners.&lt;/p&gt;

&lt;p&gt;One caveat, only one IAM role can be assigned to one EC2 instance at one time.&lt;/p&gt;

&lt;p&gt;Just add the necessary IAM role as an &lt;a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html" rel="noopener noreferrer"&gt;EC2 instance profile&lt;/a&gt;, the &lt;code&gt;AssumeRole&lt;/code&gt; wizardry will happen in the background, and the access keys will be automagically consumed by &lt;code&gt;aws-cli&lt;/code&gt; and all common AWS SDK client libraries. These are temporary access keys and they are automatically rotated in the background as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why this
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;If you are already using EC2 instances as your runners.&lt;/li&gt;
&lt;li&gt;You only need one IAM role for your pipeline.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why not this
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Your runners are not running on EC2 instance.&lt;/li&gt;
&lt;li&gt;Only one instance profile can be assigned to an EC2 instance at one time. You could use the &lt;a href="https://docs.aws.amazon.com/cli/latest/reference/ec2/associate-iam-instance-profile.html" rel="noopener noreferrer"&gt;&lt;code&gt;associate-iam-instance-profile&lt;/code&gt;&lt;/a&gt; command to update the role of a running EC2 instance but it may be challenging to manage that in a sustainable manner.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Option 3: EKS IAM Roles for service accounts (aka IRSA)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Access key type&lt;/strong&gt; - &lt;code&gt;AssumeRole&lt;/code&gt; (well technically, &lt;code&gt;AssumeRoleWithWebIdentity&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6ra4g086jpy5bpor35d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6ra4g086jpy5bpor35d.png" alt="Architecture diagram for using EKS IAM Roles for service accounts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are already using AWS EKS &lt;code&gt;&amp;gt;= 1.12&lt;/code&gt; and the &lt;a href="https://docs.gitlab.com/runner/executors/kubernetes.html" rel="noopener noreferrer"&gt;Kubernetes executor for GitLab Runner&lt;/a&gt;, this should be your preferred approach!&lt;/p&gt;

&lt;p&gt;Using the OpenID Connect (OIDC) flow, EKS acts as an OIDC Identity Provider that is trusted by IAM. The OIDC JSON Web Token (JWT) here is the JWT token of the Pod's &lt;code&gt;ServiceAccount&lt;/code&gt; which will be passed to AWS Secure Token Service (STS) with &lt;a href="https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRoleWithWebIdentity.html" rel="noopener noreferrer"&gt;&lt;code&gt;AssumeRoleWithWebIdentity&lt;/code&gt;&lt;/a&gt; and the temporary access key for the Role is returned. You can configure the IAM role trust policy to only allow specific Kubernetes namespace and Service Account to assume it.&lt;/p&gt;

&lt;p&gt;There are a few moving pieces (&lt;a href="https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html" rel="noopener noreferrer"&gt;AWS docs for IRSA&lt;/a&gt;), but essentially you need to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Enable IAM OIDC for your cluster.&lt;/li&gt;
&lt;li&gt;Associate a relevant K8S Service Account with an IAM role through annotations.&lt;/li&gt;
&lt;li&gt;Ensure the runner Pod is using said Service Account.&lt;/li&gt;
&lt;li&gt;The access keys will be automagically consumed by &lt;code&gt;aws-cli&lt;/code&gt; and all common AWS SDK client libraries.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;(If you are interested in a deep dive into what goes on behind the scenes, this is an excellent read &lt;a href="https://mjarosie.github.io/dev/2021/09/15/iam-roles-for-kubernetes-service-accounts-deep-dive.html" rel="noopener noreferrer"&gt;https://mjarosie.github.io/dev/2021/09/15/iam-roles-for-kubernetes-service-accounts-deep-dive.html&lt;/a&gt;)&lt;/p&gt;




&lt;blockquote&gt;
&lt;h3&gt;
  
  
  Pro Tip
&lt;/h3&gt;

&lt;p&gt;Remember the constraint with EC2 Instance Profile about a single role per EC2 instance? With this approach, you could create multiple SAs with relevant IAM roles, and pick the required SA per job level, with the &lt;a href="https://docs.gitlab.com/runner/executors/kubernetes.html#overwriting-kubernetes-default-service-account" rel="noopener noreferrer"&gt;&lt;code&gt;KUBERNETES_SERVICE_ACCOUNT_OVERWRITE&lt;/code&gt;&lt;/a&gt; variable in &lt;code&gt;.gitlab-ci.yml&lt;/code&gt; 😎&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;Technically, this could also be done with non-EKS Kubernetes clusters a self-deployed &lt;a href="https://github.com/aws/amazon-eks-pod-identity-webhook/blob/master/SELF_HOSTED_SETUP.md" rel="noopener noreferrer"&gt;amazon-eks-pod-identity-webhook&lt;/a&gt; but I've not tested it myself to know for sure. Maybe in a follow-up article?&lt;/p&gt;

&lt;h3&gt;
  
  
  Why this
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;If you already using the AWS EKS AND using the &lt;a href="https://docs.gitlab.com/runner/executors/kubernetes.html" rel="noopener noreferrer"&gt;Kubernetes executor for GitLab Runner&lt;/a&gt;, this is the easiest, secure and scalable way.&lt;/li&gt;
&lt;li&gt;If you are using some form of Infrastructure as Code (IaC) tool like Terraform to manage your Kubernetes and IAM resources, it will be a breeze to manage the various moving pieces.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why not this
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You are not using the Kubernetes executor for GitLab Runner.&lt;/li&gt;
&lt;li&gt;You are using a different flavor of Kubernetes - self-managed, AKS, GCP (to be confirmed if &lt;a href="https://github.com/aws/amazon-eks-pod-identity-webhook/blob/master/SELF_HOSTED_SETUP.md" rel="noopener noreferrer"&gt;amazon-eks-pod-identity-webhook&lt;/a&gt; can be deployed successfully outside EKS).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Option 4: IAM OIDC identity provider integration with GitLab
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Access key type&lt;/strong&gt; - &lt;code&gt;AssumeRole&lt;/code&gt; (similar to above, technically it's &lt;code&gt;AssumeRoleWithWebIdentity&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu7zdygxg5hrnxx04spzu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu7zdygxg5hrnxx04spzu.png" alt="Architecture diagram for using IAM OIDC identity provider integration with GitLab"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is an alternative option that is similar to Option 3, but for those who are not using the Kubernetes executor.&lt;/p&gt;

&lt;p&gt;It also uses the same OIDC flow, but this time, &lt;a href="https://docs.gitlab.com/ee/ci/cloud_services/aws/" rel="noopener noreferrer"&gt;GitLab is the OIDC Identity Provider&lt;/a&gt; that is trusted by IAM. Each GitLab job has an OIDC JWT that is accessible through the &lt;code&gt;CI_JOB_JWT_V2&lt;/code&gt; environment variable. In your script, you pass that to AWS STS with &lt;a href="https://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRoleWithWebIdentity.html" rel="noopener noreferrer"&gt;&lt;code&gt;AssumeRoleWithWebIdentity&lt;/code&gt;&lt;/a&gt;, and the temporary access key for the Role is returned. You can configure the IAM role trust policy to only allow specific GitLab group, project, branch, or tag, to assume it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why this
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;If your runners are not running on Kubernetes or EKS.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why not this
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You are not using the Kubernetes executor for your runners.&lt;/li&gt;
&lt;li&gt;You are not comfortable with &lt;strong&gt;any&lt;/strong&gt; GitLab runners registered in the allowed GitLab group, project, branch or tag to assume the IAM role.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Option 5: HashiCorp Vault
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Access key type&lt;/strong&gt; - IAM user (Plot twist, the IAM user from Vault is not static, but temporary! More below.), &lt;code&gt;AssumeRole&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F80xtuzp66aq793viqzsm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F80xtuzp66aq793viqzsm.png" alt="Architecture diagram for using HashiCorp Vault"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxhnsbjqcgnr68l9ddh4k.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxhnsbjqcgnr68l9ddh4k.gif" alt="A person opens a vault that looks like those from banks, welcoming you inside"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Welcome to The Holy Grail for secrets management.&lt;/p&gt;

&lt;p&gt;For the uninitiated, think of &lt;a href="https://www.vaultproject.io/" rel="noopener noreferrer"&gt;HashiCorp Vault&lt;/a&gt; as a broker of secrets. You (human, script, machine, etc) authenticate to Vault with a slew of &lt;a href="https://developer.hashicorp.com/vault/docs/auth" rel="noopener noreferrer"&gt;auth methods&lt;/a&gt; (depending on what's enabled and what you have been allowed to auth with), and in exchange, receive a lease to a Vault token. That token is tied to a policy that allows you to request one or more configured &lt;a href="https://developer.hashicorp.com/vault/docs/secrets" rel="noopener noreferrer"&gt;secrets&lt;/a&gt;. When you request a secret, Vault does the heavy lifting of provisioning it in the respective backend and also removing it from the respective backend when the lease to the token expires.&lt;/p&gt;

&lt;p&gt;If you already have HashiCorp Vault in your stack, it supports the &lt;a href="https://developer.hashicorp.com/vault/docs/secrets/aws" rel="noopener noreferrer"&gt;AWS Secrets Engine&lt;/a&gt; out of the box. Vault can generate IAM access keys dynamically for IAM users and IAM roles that you manage in Vault itself (more accurately, the IAM user and IAM roles are created dynamically, and with it comes the access key). &lt;/p&gt;

&lt;p&gt;One nice thing about using Vault is that all IAM access keys generated by Vault are time-based, and automatically revoked and removed from AWS once it is expired. This also includes the access token generated for IAM users, where the created IAM user itself is also removed from AWS once the time-to-live is reached (end of plot twist).&lt;/p&gt;

&lt;p&gt;In your pipeline script, you'd perform the end to end flow (notice that you won't even interact with AWS IAM / STS directly): &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Authenticate to Vault&lt;/li&gt;
&lt;li&gt;Logging in with the returned Vault token&lt;/li&gt;
&lt;li&gt;Request a new secret for the relevant IAM user or IAM role&lt;/li&gt;
&lt;li&gt;Use the generated temporary IAM access key to call AWS&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Why this
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Vault is the most secure option and offers more than one way for your runners to authenticate - &lt;a href="https://developer.hashicorp.com/vault/docs/auth/cert" rel="noopener noreferrer"&gt;TLS certs&lt;/a&gt;, &lt;a href="https://developer.hashicorp.com/vault/docs/auth/kubernetes" rel="noopener noreferrer"&gt;Kubernetes Service Account&lt;/a&gt;, &lt;a href="https://developer.hashicorp.com/vault/docs/auth/jwt" rel="noopener noreferrer"&gt;OIDC&lt;/a&gt; and &lt;a href="https://developer.hashicorp.com/vault/docs/auth" rel="noopener noreferrer"&gt;many more&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Your runners can run from anywhere as long as they can reach Vault.&lt;/li&gt;
&lt;li&gt;Once you have Vault in your stack, you can extend the dynamic secrets pattern to other applications, machines, and humans.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why not this
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It's harder to setup and integrate Vault in your existing stack if you don't have it already.&lt;/li&gt;
&lt;li&gt;If you are &lt;em&gt;only&lt;/em&gt; using Vault the AWS Secrets Engine for the runners in the foreseeable future, the other options may be better ROI for the time and effort spent to get a production ready Vault cluster up.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Big caveats before you adopt this
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Make sure you have a secure way for your runners to authenticate with Vault, TLS (mTLS if possible), and strong credentials. Otherwise, any authenticated and authorized Vault tokens can generate valid AWS access tokens.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Which option is the best for me?
&lt;/h2&gt;

&lt;p&gt;Unfortunately, it depends 😅&lt;/p&gt;

&lt;p&gt;It depends on your context and your use case. You may start with Option 1 to get started quicker and then migrate to another more secure approach, or even have more than one option, for different workloads.&lt;/p&gt;

&lt;p&gt;Hopefully I was able to help shed light on some clarifying questions you can ask to help decide the best option for you.&lt;/p&gt;

&lt;p&gt;But the best option sometimes is the one you finally choose - the quicker you pick one, the quicker your pipeline can deliver value to your end users (securely of course).&lt;/p&gt;

&lt;p&gt;All the best!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwskc64piwv3lg2jvqr8m.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwskc64piwv3lg2jvqr8m.gif" alt="Katniss Everdeen with the respect sign"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;I hope you enjoyed my first post on DEV 🥹&lt;/p&gt;

&lt;p&gt;Either way, I would appreciate it if you will let me know below what you think of this article - if it helps you, anything you agree or disagree with, and what follow-up articles you would like to see. Looking forward to your comments!&lt;/p&gt;

</description>
      <category>devops</category>
      <category>aws</category>
      <category>gitlab</category>
      <category>security</category>
    </item>
  </channel>
</rss>
