Create a Pulumi learning environment

Add files for a Vagrant-backed Pulumi learning environment. Add files for using Pulumi to spin up an EC2 instance. Environment is not currently functional.

Signed-off-by: Scott Lowe <scott.lowe@scottlowe.org>
This commit is contained in:
Scott Lowe 2019-04-21 14:13:26 -06:00
parent f244f5b5e4
commit 50f996346d
No known key found for this signature in database
GPG key ID: 949F43F6E6C11780
9 changed files with 229 additions and 0 deletions

0
pulumi/README.md Normal file
View file

82
pulumi/Vagrantfile vendored Normal file
View file

@ -0,0 +1,82 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Specify minimum Vagrant version and Vagrant API version
Vagrant.require_version '>= 1.6.0'
VAGRANTFILE_API_VERSION = '2'
# Require 'yaml' module
require 'yaml'
# Read YAML file with VM details (box, CPU, RAM, IP addresses)
# Edit machines.yml to change VM configuration details
machines = YAML.load_file(File.join(File.dirname(__FILE__), 'machines.yml'))
# Create and configure the VMs
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Always use Vagrant's default insecure key
config.ssh.insert_key = false
# Iterate through entries in YAML file to create VMs
machines.each do |machine|
# Configure the VMs per details in machines.yml
config.vm.define machine['name'] do |srv|
# Don't check for box updates
srv.vm.box_check_update = false
# Specify the hostname of the VM
srv.vm.hostname = machine['name']
# Specify the Vagrant box to use (use VMware box by default)
srv.vm.box = machine['box']['vmw']
# Configure default synced folder (disable by default)
if machine['sync_disabled'] != nil
srv.vm.synced_folder '.', '/vagrant', disabled: machine['sync_disabled']
else
srv.vm.synced_folder '.', '/vagrant', disabled: true
end #if machine['sync_disabled']
# Assign additional private network
if machine['ip_addr'] != nil
srv.vm.network 'private_network', ip: machine['ip_addr']
end # if machine['ip_addr']
# Configure CPU & RAM per settings in machines.yml (Fusion)
srv.vm.provider 'vmware_fusion' do |vmw|
vmw.vmx['memsize'] = machine['ram']
vmw.vmx['numvcpus'] = machine['vcpu']
if machine['nested'] == true
vmw.vmx['vhv.enable'] = 'TRUE'
end #if machine['nested']
end # srv.vm.provider 'vmware_fusion'
# Configure CPU & RAM per settings in machines.yml (VirtualBox)
srv.vm.provider 'virtualbox' do |vb, override|
vb.memory = machine['ram']
vb.cpus = machine['vcpu']
override.vm.box = machine['box']['vb']
vb.customize ['modifyvm', :id, '--nictype1', 'virtio']
vb.customize ['modifyvm', :id, '--nictype2', 'virtio']
end # srv.vm.provider 'virtualbox'
# Configure CPU & RAM per settings in machines.yml (Libvirt)
srv.vm.provider 'libvirt' do |lv,override|
lv.memory = machine['ram']
lv.cpus = machine['vcpu']
override.vm.box = machine['box']['lv']
if machine['nested'] == true
lv.nested = true
end # if machine['nested']
end # srv.vm.provider 'libvirt'
end # config.vm.define
end # machines.each
# Provision the VM with Ansible
config.vm.provision 'ansible' do |ansible|
ansible.playbook = 'provision.yml'
end # config.vm.provision
end # Vagrant.configure

6
pulumi/ansible.cfg Normal file
View file

@ -0,0 +1,6 @@
[defaults]
inventory = ./.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory
private_key_file = ~/.vagrant.d/insecure_private_key
remote_user = vagrant
host_key_checking = False
retry_files_enabled = False

View file

@ -0,0 +1,8 @@
name: ec2instance
runtime: nodejs
description: Basic example of an AWS EC2 instance
template:
config:
aws:region:
description: The AWS region to deploy into
default: us-west-2

View file

@ -0,0 +1,34 @@
"use strict";
const aws = require("@pulumi/aws");
// Specify instance size/type
let size = "t2.micro"; // t2.micro is available in the AWS free tier
// Specify AMI
let ami = "ami-c55673a0"; // AMI for Ubuntu 16.04 in us-west-2 (Oregon)
// Create a new security group for port 80
let group = new aws.ec2.SecurityGroup("pulumi-secgrp", {
ingress: [
{ protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: ["0.0.0.0/0"] },
{ protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] },
],
});
// (optional) create a simple web server using the startup script for the instance
let userData =
`#!/bin/bash
echo "Hello, World!" > index.html
nohup python -m SimpleHTTPServer 80 &`;
let server = new aws.ec2.Instance("pulumi-ubuntu", {
tags: { "Name": "pulumi-ubuntu" },
instanceType: size,
securityGroups: [ group.name ], // reference the group object above
ami: ami,
userData: userData // start a simple web server
});
exports.publicIp = server.publicIp;
exports.publicHostName = server.publicDns;

View file

@ -0,0 +1,9 @@
{
"name": "ec2instance",
"version": "0.1.0",
"main": "index.js",
"dependencies": {
"@pulumi/pulumi": "latest",
"@pulumi/aws": "latest"
}
}

View file

@ -0,0 +1,22 @@
---
- hosts: "all"
remote_user: "vagrant"
tasks:
- name: "Create directory to store files"
file:
state: "directory"
path: "/home/vagrant/pulumi"
owner: "vagrant"
group: "vagrant"
- name: "Copy files into sandbox environment"
copy:
src: "{{ item }}"
dest: "/home/vagrant/pulumi"
owner: "vagrant"
group: "vagrant"
with_items:
- "index.js"
- "package.json"
- "Pulumi.yaml"

9
pulumi/machines.yml Normal file
View file

@ -0,0 +1,9 @@
---
- box:
vmw: "generic/ubuntu1604"
vb: "ubuntu/xenial64"
lv: "generic/ubuntu1604"
name: "xenial"
nested: false
ram: "1024"
vcpu: "1"

59
pulumi/provision.yml Normal file
View file

@ -0,0 +1,59 @@
---
- hosts: "all"
become: "yes"
remote_user: "vagrant"
gather_facts: false
pre_tasks:
- name: "Install Python 2 if not present"
raw: "apt -qqy update; apt -qqy install python"
register: output
retries: 3
delay: 3
until: output.rc == 0
tasks:
- name: "Ensure /usr/local/bin exists"
file:
state: "directory"
path: "/usr/local/bin"
owner: "root"
group: "root"
- name: "Check for existence of Pulumi binary in /usr/local/bin"
stat:
path: "/usr/local/bin/pulumi"
register: pulumi_present
- name: Install NodeJS
apt:
state: "present"
name: "{{ item }}"
with_items:
- nodejs
- npm
- block:
- name: "Download and extract Pulumi 0.17.5"
unarchive:
src: "https://get.pulumi.com/releases/sdk/pulumi-v0.17.5-linux-x64.tar.gz"
remote_src: true
dest: "/tmp"
owner: "root"
group: "root"
- name: "Move binaries to /usr/local/bin"
copy:
src: "/tmp/pulumi/{{ item }}"
remote_src: true
dest: "/usr/local/bin"
owner: "root"
group: "root"
mode: 0755
with_items:
- "pulumi"
- "pulumi-language-go"
- "pulumi-language-nodejs"
- "pulumi-language-python"
- "pulumi-language-python-exec"
- "pulumi-resource-pulumi-nodejs"
when: pulumi_present.stat.exists == False