| 1 |
// These can split into separate files, come from environment variables / commandline etc., as |
| 2 |
// desired. |
| 3 |
local aws_region = 'us-east-1'; |
| 4 |
local key_name = 'key_name'; |
| 5 |
local key_path = 'key_path'; |
| 6 |
local access_key = 'XXX'; |
| 7 |
local secret_key = 'YYY'; |
| 8 |
|
| 9 |
// Ubuntu Precise 12.04 LTS (x64) |
| 10 |
local aws_amis = { |
| 11 |
'eu-west-1': 'ami-b1cf19c6', |
| 12 |
'us-east-1': 'ami-de7ab6b6', |
| 13 |
'us-west-1': 'ami-3f75767a', |
| 14 |
'us-west-2': 'ami-21f78e11', |
| 15 |
}; |
| 16 |
|
| 17 |
|
| 18 |
{ |
| 19 |
// Specify the provider and access details |
| 20 |
provider: { |
| 21 |
aws: { |
| 22 |
region: aws_region, |
| 23 |
access_key: access_key, |
| 24 |
secret_key: secret_key, |
| 25 |
}, |
| 26 |
}, |
| 27 |
|
| 28 |
resource: { |
| 29 |
aws_security_group: { |
| 30 |
// Our default security group to access |
| 31 |
// the instances over SSH and HTTP |
| 32 |
default: { |
| 33 |
name: 'terraform_example', |
| 34 |
description: 'Used in the terraform', |
| 35 |
|
| 36 |
ingress: [ |
| 37 |
// SSH access from anywhere |
| 38 |
{ |
| 39 |
from_port: 22, |
| 40 |
to_port: 22, |
| 41 |
protocol: 'tcp', |
| 42 |
cidr_blocks: ['0.0.0.0/0'], |
| 43 |
}, |
| 44 |
// HTTP access from anywhere |
| 45 |
{ |
| 46 |
from_port: 80, |
| 47 |
to_port: 80, |
| 48 |
protocol: 'tcp', |
| 49 |
cidr_blocks: ['0.0.0.0/0'], |
| 50 |
}, |
| 51 |
], |
| 52 |
}, |
| 53 |
}, |
| 54 |
|
| 55 |
aws_elb: { |
| 56 |
web: { |
| 57 |
name: 'terraform-example-elb', |
| 58 |
|
| 59 |
// The same availability zone as our instance |
| 60 |
availability_zones: ['${aws_instance.web.availability_zone}'], |
| 61 |
|
| 62 |
listener: { |
| 63 |
instance_port: 80, |
| 64 |
instance_protocol: 'http', |
| 65 |
lb_port: 80, |
| 66 |
lb_protocol: 'http', |
| 67 |
}, |
| 68 |
|
| 69 |
// The instance is registered automatically |
| 70 |
instances: ['${aws_instance.web.id}'], |
| 71 |
}, |
| 72 |
}, |
| 73 |
|
| 74 |
aws_instance: { |
| 75 |
web: { |
| 76 |
// The connection block tells our provisioner how to |
| 77 |
// communicate with the resource (instance) |
| 78 |
connection: { |
| 79 |
// The default username for our AMI |
| 80 |
user: 'ubuntu', |
| 81 |
|
| 82 |
// The path to your keyfile |
| 83 |
key_file: key_path, |
| 84 |
}, |
| 85 |
|
| 86 |
instance_type: 'm1.small', |
| 87 |
|
| 88 |
// Lookup the correct AMI based on the region |
| 89 |
// we specified |
| 90 |
ami: aws_amis[aws_region], |
| 91 |
|
| 92 |
// The name of our SSH key pair you've created and downloaded |
| 93 |
// from the AWS console. |
| 94 |
// |
| 95 |
// https://console.aws.amazon.com/ec2/v2/home?region=us-west-2#KeyPairs: |
| 96 |
// |
| 97 |
key_name: key_name, |
| 98 |
|
| 99 |
// Our Security group to allow HTTP and SSH access |
| 100 |
security_groups: [$.resource.aws_security_group.default.name], |
| 101 |
|
| 102 |
// We run a remote provisioner on the instance after creating it. |
| 103 |
// In this case, we just install nginx and start it. By default, |
| 104 |
// this should be on port 80 |
| 105 |
provisioner: { |
| 106 |
'remote-exec': { |
| 107 |
inline: [ |
| 108 |
'sudo apt-get -y update', |
| 109 |
'sudo apt-get -y install nginx', |
| 110 |
'sudo service nginx start', |
| 111 |
], |
| 112 |
}, |
| 113 |
}, |
| 114 |
}, |
| 115 |
}, |
| 116 |
|
| 117 |
}, |
| 118 |
|
| 119 |
output: { |
| 120 |
address: { |
| 121 |
value: '${aws_elb.web.dns_name}', |
| 122 |
}, |
| 123 |
}, |
| 124 |
} |