Deploying Cloud Resources with Pulumi and LocalStack

Pulumi AWS LocalStack golang

2 min read | by Jordi Prats

Pulumi is a powerful infrastructure as code tool that allows developers to deploy and manage cloud resources using familiar programming languages. However, when it comes to using Pulumi with LocalStack, there are some changes that need to be made to make Pulumi be able to reach LocalStack.

We will be utilizing the following Golang code (full project available in github) to create an S3 bucket on AWS, although our intention is to use it to create a bucket on LocalStack:

package main

import (
  "github.com/pulumi/pulumi-aws/sdk/v4/go/aws/s3"
  "github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
  pulumi.Run(func(ctx *pulumi.Context) error {
    bucket, err := s3.NewBucket(ctx, "my-bucket", nil)
    if err != nil {
      return err
    }

    ctx.Export("bucketName", bucket.Bucket)
    return nil
  })
}

In order to configure pulumi to reach LocalStack we'll have to edit the Pulumi.test.yaml file with the following:

config:
  aws:region: us-east-1
  aws:accessKey: test
  aws:secretKey: test
  aws:endpoints:
    - s3: http://localhost:4566
    - cloudwatch: http://localhost:4566
    - cloudwatchlogs: http://localhost:4566
    -  dynamodb: http://localhost:4566
    - iam: http://localhost:4566
    - lambda: http://localhost:4566
    - secretsmanager: http://localhost:4566
    - sns: http://localhost:4566
    - sqs: http://localhost:4566
  aws:s3_force_path_style: true
  aws:skipCredentialsValidation: true
  aws:skipRequestingAccountId: true

For this example we just really need the s3 endpoint. With this file updated, we can use pulumi up as usual:

$ pulumi up
Previewing update (test)

View in Browser (Ctrl+O): https://app.pulumi.com/jordiprats/s3-demo/test/previews/17671359-b569-449a-ac12-6be5bb15c33b

     Type                 Name          Plan
     pulumi:pulumi:Stack  s3-demo-test
 +   └─ aws:s3:Bucket     my-bucket     create


Outputs:
  + bucketName: "my-bucket-0035d8a"

Resources:
    + 1 to create
    1 unchanged

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

View in Browser (Ctrl+O): https://app.pulumi.com/jordiprats/s3-demo/test/updates/2

     Type                 Name          Status
     pulumi:pulumi:Stack  s3-demo-test
 +   └─ aws:s3:Bucket     my-bucket     created (0.41s)


Outputs:
  + bucketName: "my-bucket-d016c02"

Resources:
    + 1 created
    1 unchanged

Duration: 4s

We can validate that it has created the S3 bucket using LocalStack by checking it with awslocal:

$ awslocal s3 ls
2023-04-22 12:02:34 my-bucket-d016c02

Posted on 25/04/2023