Learn Pulumi for AWS by Creating an Email List
Pulumi is an Infrastructure-as-Code newcomer that lets you use actual code define your servers, services, etc., on platforms like AWS, Google Cloud, and Azure. Right now it allows JavaScript, Typescript, and Python.
Personally, I love using TypeScript because I get type checking and popups in VSCode that show me how to properly set up things.
I also like Pulumi because it has wrappers around some of the more complicated platform features.
Some of the wrappers are even facades around services that are similar between platforms. For instance, setting up a Lambda function in AWS and a Google Cloud Function both look like this:
import cloud from '@pulumi/cloud';
const api = new cloud.API('my-api');
api.get('/hello', (req, res) => {
res.send(`Hello, ${req.params.name}`);
});
expor const url = api.publish().url;
Not bad, right? Pulumi also has some AWS-specific wrappers too. Here's an example of running a couple nginx containers on Fargate and then fronting them with a NLB listener that automatically routes the traffic.
import * as pulumi from 'pulumi/pulumi';
import * as aws from '@pulumi/aws';
import * as awsx from "@pulumi/awsx";
// Create an elastic network listener to listen for requests and route them to the container.
let listener = new awsx.elasticloadbalancingv2.NetworkListener("nginx", { port: 80 });
// Define the service to run. We pass in the listener to hook up the network load balancer
// to the containers the service will launch.
let service = new awsx.ecs.FargateService("nginx", {
desiredCount: 2,
taskDefinitionArgs: {
containers: {
nginx: {
image: 'nginx',
memory: 512,
portMappings: [listener],
},
},
},
});
// export just the hostname property of the container frontend
export const hostname = listener.endpoint.apply(e => `http://${e.hostname}`);
If any of this looks confusing, remember that you can use your editor to peek TypeScript definitions. The pulumi docs, tutorials, and examples are also] excellent resources. If you get stuck, they also have a slack channel where you can ask questions.
This is what peeking (option+F12) the definition of awsx.ecs.FargateService
looks like in VSCode.
Learn Pulumi by Doing
There's a post out on Medium by Derek Woods that explains AWS Serverless by creating a basic email signup list. I want to do the same with Pulumi, and show you how easy it is.
Install
First install pulumi. It's easy if you trust curling their bash script:
$ curl -fsSL https://get.pulumi.com | sh
Otherwise you can use brew on macOS:
$ brew install pulumi
Pulumi by default will use your existing aws credentials. If you haven't set them up, download the aws cli and set your access key and access secret.