How To Make Your AWS EKS Cluster Use Fargate Using Pulumi And Golang
Tags: go, devops, kubernetes
Published: 2020-01-18 01:37:09

---
title: How To Make Your AWS EKS Cluster Use Fargate Using Pulumi And Golang
published: true
date: 2020-01-16 00:00:00 UTC
tags: Go,DevOps,Kubernetes
canonical_url: https://retgits.com/2020/01/how-to-make-your-aws-eks-cluster-use-fargate-using-pulumi-and-golang/
---

At re:Invent, [AWS introduced the ability](https://aws.amazon.com/blogs/aws/amazon-eks-on-aws-fargate-now-generally-available/) to have EKS run pods on AWS Fargate, and Fargate is [cheaper than hosting Kubernetes yourself](https://www.cloudjourney.io/articles/publiccloud/cost-matters-the-serverless-edition-ls/). In the last [post](https://retgits.com/2020/01/how-to-create-an-aws-eks-cluster-using-pulumi-and-golang/) I created an EKS cluster, so let’s add this new capability to the cluster and remove the need to manage or provision infrastructure for our pods.

The complete project is available on [GitHub](https://github.com/retgits/pulumi-blogs/tree/fargate-blog).

## Configuration

The minimum configuration for the Fargate profile is a name, the Kubenetes namespace it’ll work in, and the IAM role it needs to run the pods on AWS Fargate. The configuration below, which you can copy/paste into the YAML file from the previous blog, has three parameters. The parameter `fargate:profile-name` is the name of the Fargate profile, the parameter `fargate:namespace` is the Kubernetes namespace, and `fargate:execution-role-arn` is the ARN of the IAM role. For more details on how to create the role, check out the [AWS docs](https://docs.aws.amazon.com/eks/latest/userguide/pod-execution-role.html).

```yaml
fargate:profile-name: EKSFargateProfile
fargate:namespace: example
fargate:execution-role-arn: "arn:aws:iam::ACCOUNTID:role/EKSFargatePodExecutionRole"

```

You can either use the command line, like `pulumi config set fargate:profile-name "EKSFargateProfile"` to add these new configuration variables, or you can add them directly into the yaml file. The yaml file with all the configuration is called `Pulumi.<name of your project>.yaml`.

### Adding the Fargate profile

The code below is an extension from the code created in the previous [post](https://retgits.com/2020/01/how-to-create-an-aws-eks-cluster-using-pulumi-and-golang/). So you can copy/paste this snippet into your Go code too. Walking through the code, it gets the name of the profile and the namespace from the YAML file. The `fargateProfileArgs` the name of the [cluster](https://retgits.com/2020/01/how-to-create-an-aws-eks-cluster-using-pulumi-and-golang/) and [subnets](https://retgits.com/2020/01/how-to-add-subnets-to-a-vpc-in-aws-using-pulumi-and-golang/) from previous blog posts so check those out if you haven’t already. The call to `eks.NewFargateProfile()` adds the Fargate profile to your EKS cluster.

```go
// Create an EKS Fargate Profile
fargateProfileName := getEnv(ctx, "fargate:profile-name", "unknown")

selectors := make([]map[string]interface{}, 1)
namespaces := make(map[string]interface{})
namespaces["namespace"] = getEnv(ctx, "fargate:namespace", "unknown")
selectors[0] = namespaces

fargateProfileArgs := &eks.FargateProfileArgs{
    ClusterName: clusterName,
    FargateProfileName: fargateProfileName,
    Tags: tags,
    SubnetIds: subnets["subnet_ids"],
    Selectors: selectors,
    PodExecutionRoleArn: getEnv(ctx, "fargate:execution-role-arn", "unknown"),
}

fargateProfile, err := eks.NewFargateProfile(ctx, fargateProfileName, fargateProfileArgs)
if err != nil {
    fmt.Println(err.Error())
    return err
}

ctx.Export("FARGATE-PROFILE-ID", fargateProfile.ID())

```

## Running the code

Like the previous time, the last thing to do is run `pulumi up` to tell Pulumi to go add the Fargate profile to your EKS cluster! If you’re using the same project and stack, Pulumi will automatically realize it needs to add the profile to the existing cluster and won’t create a new EKS cluster.

```bash
$ pulumi up
Previewing update (builderstack):

     Type Name Plan       
     pulumi:pulumi:Stack builder-builderstack             
 + └─ aws:eks:FargateProfile EKSFargateProfile create     
 
Outputs:
  + FARGATE-PROFILE-ID: output<string>

Resources:
    + 1 to create
    5 unchanged

Do you want to perform this update? yes
Updating (builderstack):

     Type Name Status      
     pulumi:pulumi:Stack builder-builderstack              
 + └─ aws:eks:FargateProfile EKSFargateProfile created     
 
Outputs:
    CLUSTER-ID : "myEKSCluster"
  + FARGATE-PROFILE-ID: "myEKSCluster:EKSFargateProfile"
    SUBNET-IDS : [
        [0]: "subnet-0a1909bec2e936bd7"
        [1]: "subnet-09d229c2eb8061979"
    ]
    VPC-ID : "vpc-0437c750acf1050c3"

Resources:
    + 1 created
    5 unchanged

Duration: 2m27s

Permalink: https://app.pulumi.com/retgits/builder/builderstack/updates/4

```

The permalink at the bottom of the output takes you to the Pulumi console where you can see all the details of the execution of your app and the resources that were created.

![The Pulumi console also has really useful links to the AWS console to see the resources.](https://retgits.com/images/how-to-make-your-aws-eks-cluster-use-fargate-using-pulumi-and-golang-1.png) <figcaption>
            <p>The Pulumi console also has really useful links to the AWS console to see the resources.</p>
        </figcaption>