published: 3rd of September 2022
Pulumi is an Infrastructure as Code (IaC) tool similar to Terraform, except that, rather than defining resources in a DSL, you defined them in a supported programming language. This means you have access to all of your chosen languages features as well as any supporting libraries you may need.
In this post I will cover the steps to get started configuring your AWS environment with Pulumi and the Go language.
The following software was used in this post.
An AWS user is required to interact with AWS resources.
Create an IAM user that will access AWS programatically. The docs to create a user can be found here.
Once the user is created, generate access keys. The relevant docs can be found here.
Add the credentials as environment variables in your ~/.zshrc file. This allows Pulumi to utilize the credentials automagically.
# ~/.zshrc
export AWS_ACCESS_KEY_ID="<access-key>"
export AWS_SECRET_ACCESS_KEY="<secret-key>"
export AWS_REGION="<region>"
I am using Pulumi with the Go language so we need to have Go installed.
Details for installing Go can be found in the docs here.
The TL/DR steps are as follows.
# Download
wget https://go.dev/dl/go1.19.linux-amd64.tar.gz
# Extract
tar -zxvf go1.19.linux-amd64.tar.gz
# Install
sudo mv go/ /usr/local
Add Go to your path via your ~/.zshrc file. This gives you access to the go cli command.
# ~/.zshrc
export PATH=$PATH:/usr/local/go/bin
Details for installing Pulumi can be found in the here.
The TL/DR with the installer script is as follows.
curl -fsSL https://get.pulumi.com | sh
Add Pulumi to your path via your ~/.zshrc file. This gives you access to the pulumi cli command.
# ~/.zshrc
export PATH=$PATH:$HOME/.pulumi/bin
I am utilising the Pulumi service backend, if you don't have an account already go here to create one.
Once you have created an account, login and create an access token that will be used to authenticate you to the Pulumi service. Browse to:
Add the Pulumi access token to your ~/.zshrc file.
# ~/.zshrc
export PULUMI_ACCESS_TOKEN="<pulumi-token>"
Alright, with that out of the way, lets move onto testing out setup by buiding some AWS resources.
Let's build some simple infrastucture in AWS with Pulumi.
Pulumi has a number of components that are used to define and manage infrastructure.
In this section I will concentrate on the following three.
When creating a new project the folder name is used for the project name. The project folder will also have a Pulumi.yaml file.
Create a project named testing123 .
mkdir testing123 && cd testing123
Use the pulumi new command to initialize the project and stack.
pulumi new aws-go --stack test --yes
The options specified are as follows.
The above command creates to following files.
The default main.go file defines the code to build and S3 bucket.
Open up the main.go file and let step through it. I have added some additional comments below to furhter explain what is happening.
// file: main.go
// Defines this file as being part of the `main` package.
package main
// Import the required packages to build the resources.
import (
"github.com/pulumi/pulumi-aws/sdk/v5/go/aws/s3"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
// Entrypoint for the program.
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
// Create an AWS resource (S3 Bucket)
bucket, err := s3.NewBucket(ctx, "my-bucket", nil)
// If we get an erorr, return the error.
if err != nil {
return err
}
// Export the name of the bucket.
// Exports are displayed as `Outputs` when
// building resources.
ctx.Export("bucketName", bucket.ID())
// exit the program returning `nil`
return nil
})
}
We are using the s3 module so we need to install it.
go get github.com/pulumi/pulumi-aws/sdk/v5/go/aws/s3
Lets build the resources by running the pulumi up command.
pulumi up -f
# Output
Updating (test)
View Live: https://app.pulumi.com/<account-name>/testing123/test/updates/3
Type Name Status
+ pulumi:pulumi:Stack testing123-test created
+ └─ aws:s3:Bucket my-bucket created
Outputs:
bucketName: "my-bucket-22610e5"
Resources:
+ 2 created
Duration: 10s
We can see from the Outputs section that the my-bucket-22610e5 S3 Bucket was created.
If you have the AWS CLI installed you can get a list of the S3 buckets with the aws s3 ls command.
aws s3 ls --region us-east-1
# Output
2022-09-03 13:16:13 my-bucket-22610e5
Otherwise, you can browse to the S3 services section in the AWS console.
Resources can be deleted with the pulumi destroy command.
pulumi destroy -f
# Output
Destroying (test)
View Live: https://app.pulumi.com/<account-name>/testing123/test/updates/4
Type Name Status
- pulumi:pulumi:Stack testing123-test deleted
- └─ aws:s3:Bucket my-bucket deleted
Outputs:
- bucketName: "my-bucket-22610e5"
Resources:
- 2 deleted
Duration: 5s
If the stack is no longer required, you can delete the stack as well with the pulumi stack rm test command.
pulumi stack rm test
# Output
This will permanently remove the 'test' stack!
Please confirm that this is what you'd like to do by typing `test`: test
Stack 'test' has been removed!
How much fun was that!
In this post I showed you how to get started managing AWS infrastructure using Pulumi with the Go language. Look out for a future post where I will show you how to host a static website on AWS with Pulumi.
https://www.pulumi.com/docs/get-started/install/
https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html#id_users_create_console
https://www.pulumi.com/docs/get-started/aws/begin/
https://www.pulumi.com/docs/intro/concepts/