· 8 years ago · Dec 19, 2017, 12:06 PM
1# make sure that you have your public/private key pair created
2# Command:
3# jantoth@debian:~$ ssh-keygen -f mykey
4
5
6# files that should be present in your directory
7# -rw-r--r-- 1 jantoth jantoth 590 Dec 16 09:14 instance.tf
8# -rw------- 1 jantoth jantoth 1679 Dec 16 15:07 mykey
9# -rw-r--r-- 1 jantoth jantoth 410 Dec 16 15:07 mykey.pub
10# -rw-r--r-- 1 jantoth jantoth 135 Dec 16 09:14 provider.tf
11# -rw-r--r-- 1 jantoth jantoth 224 Dec 16 09:14 script.sh
12# -rw-r--r-- 1 jantoth jantoth 103 Dec 16 15:05 terraform.tfvars
13# -rw-r--r-- 1 jantoth jantoth 366 Dec 16 15:24 vars.tf
14
15
16# *****************************************
17# * *
18# * file: instance.tf *
19# * *
20# *****************************************
21
22jantoth@debian:~$ cat instance.tf
23
24...
25resource "aws_key_pair" "mykey" {
26 key_name = "mykey"
27 public_key = "${file("${var.PATH_TO_PUBLIC_KEY}")}"
28}
29
30resource "aws_instance" "example" {
31 ami = "${lookup(var.AMIS, var.AWS_REGION)}"
32 instance_type = "t2.micro"
33 key_name = "${aws_key_pair.mykey.key_name}"
34
35 provisioner "file" {
36 source = "script.sh"
37 destination = "/tmp/script.sh"
38 }
39 provisioner "remote-exec" {
40 inline = [
41 "chmod +x /tmp/script.sh",
42 "sudo /tmp/script.sh"
43 ]
44 }
45 connection {
46 user = "${var.INSTANCE_USERNAME}"
47 private_key = "${file("${var.PATH_TO_PRIVATE_KEY}")}"
48 }
49}
50...
51
52:wq!
53
54# *****************************************
55# * *
56# * file: provider.tf *
57# * *
58# *****************************************
59
60jantoth@debian:~$ cat provider.tf
61
62...
63provider "aws" {
64 access_key = "${var.AWS_ACCESS_KEY}"
65 secret_key = "${var.AWS_SECRET_KEY}"
66 region = "${var.AWS_REGION}"
67}
68...
69:wq!
70
71
72# *****************************************
73# * *
74# * file: terraform.tfvars *
75# * *
76# *****************************************
77
78jantoth@debian:~$ cat terraform.tfvars
79
80...
81AWS_ACCESS_KEY = "AKI...WQ"
82AWS_SECRET_KEY = "DU2...k6X"
83...
84:wq!
85
86
87# *****************************************
88# * *
89# * file: vars.tf *
90# * *
91# *****************************************
92
93jantoth@debian:~$ cat vars.tf
94
95...
96variable "AWS_ACCESS_KEY" {}
97variable "AWS_SECRET_KEY" {}
98variable "AWS_REGION" {
99 default = "eu-central-1"
100}
101variable "AMIS" {
102 type = "map"
103 default = {
104 eu-central-1 = "ami-13b8337c"
105 }
106}
107
108variable "PATH_TO_PRIVATE_KEY" {
109 default = "mykey"
110}
111variable "PATH_TO_PUBLIC_KEY" {
112 default = "mykey.pub"
113}
114variable "INSTANCE_USERNAME" {
115 default = "ubuntu"
116}
117
118...
119:wq!
120
121
122# *****************************************
123# * *
124# * file: script.sh *
125# * *
126# *****************************************
127
128jantoth@debian:~$ cat script.sh
129
130...
131#!/bin/bash
132
133# sleep until instance is ready
134until [[ -f /var/lib/cloud/instance/boot-finished ]]; do
135 sleep 1
136done
137
138# install nginx
139apt-get update
140apt-get -y install nginx
141
142# make sure nginx is started
143service nginx start
144...
145
146 :wq!