diff --git a/.terraform.lock.hcl b/.terraform.lock.hcl new file mode 100644 index 0000000..a7373fa --- /dev/null +++ b/.terraform.lock.hcl @@ -0,0 +1,20 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "3.57.0" + hashes = [ + "h1:H6JCnoa3swF3rgHL0ys9KNArffU+IEGPvhQ6JnfQY/c=", + "zh:241a4203078ea35f63202b613f0e4b428a842734ded62d9f487cdf7c2a66d639", + "zh:2c1cbf3cd03a2a7ff267be09cedf1698738c372b1411ca74cfcb3bf4b0846f27", + "zh:318ad2331f60e03d284f90f728486b9df7ac9570af641c43b56216357e624b52", + "zh:43ff96b34b4829a34693281492786b9ca6dd06870dd45b0ae82ea352c33353d7", + "zh:6c36b874622603793fc637272742d84ecbf68dfe4c8d8148bb6e9b733cd0e216", + "zh:7a1aaac01c82d06f9ebc997ae2094a7d96e7a467aaaeaa1cda64ee952f3144d8", + "zh:9b917b03b8771f87a021fe7aa9fd00ae06cc455a1eaa1fb748930182617b2772", + "zh:bd90550e6d9311092170f4935e42e91e6d8bed5241e41eca39fa4aeca28d9c6f", + "zh:be5076ea705c174581fd616b118e0c17d15bd8ab0da1b3eee4f3fb6b11e78f2c", + "zh:f4f0d13414c932ecf65ba92daab6e755c244dcb77b4be59a3ac18ba2f56cdc00", + "zh:fa3575a23fd20ce00658977374491022c4c0c36a00260ebeebb0c3f3af4824aa", + ] +} diff --git a/05_multi-provider/main.tf b/05_multi-provider/main.tf index f4a71f8..6c558c4 100644 --- a/05_multi-provider/main.tf +++ b/05_multi-provider/main.tf @@ -24,6 +24,17 @@ provider "aws" { data "github_ip_ranges" "test" { } +data "aws_ami" "ubuntu_16_04" { + most_recent = true + + filter { + name = "name" + values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"] + } + + owners = ["099720109477"] +} + resource "aws_security_group" "training" { name_prefix = var.namespace @@ -40,7 +51,8 @@ resource "aws_security_group" "training" { protocol = "-1" cidr_blocks = ["0.0.0.0/0"] - #cidr_blocks = data.github_ip_ranges.test.pages + #cidr_blocks = slice (data.github_ip_ranges.test.pages, 0, 5) + #ipv6_cidr_blocks = slice (data.github_ip_ranges.test.pages, 6, length(data.github_ip_ranges.test.pages)) } } @@ -50,7 +62,7 @@ resource "aws_key_pair" "training" { } resource "aws_instance" "example" { - ami = var.ami + ami = data.aws_ami.ubuntu_16_04.image_id instance_type = "t2.micro" vpc_security_group_ids = [aws_security_group.training.id] diff --git a/README.md b/README.md index 336ef35..fc60521 100644 --- a/README.md +++ b/README.md @@ -2,17 +2,8 @@ Before and after code for a _Terraform Introduction_ class. -## Terraform Cloud +## Branches -This branch is for use with a student's own AWS credentials and is intended to run on Terraform Cloud (but can also run locally). +See the `after` branch for the completed code from the class. -Major changes: - -- A security group is created for SSH and HTTP -- The configuration only uses resources built by the configuration itself -- `public_key` and `private_key` should be provided as contents rather than a path to a file. -- AWS credentials should be provided as environment variables. - - export AWS_ACCESS_KEY_ID="AAAA" - export AWS_SECRET_ACCESS_KEY="AAAA" - export AWS_DEFAULT_REGION="us-west-2" +See the `after-byoaws` branch for the completed code that can be run on a student's own AWS account (instead of on the instructor-created AWS account). diff --git a/getting-started/azure/README.md b/getting-started/azure/README.md new file mode 100644 index 0000000..c92f3f3 --- /dev/null +++ b/getting-started/azure/README.md @@ -0,0 +1,9 @@ +# Getting Started with Terraform on Azure + +This code accompanies the [Getting Started](https://learn.hashicorp.com/terraform/azure/intro_az) guide on [learn.hashicorp.com](https://learn.hashicorp.com/). + +Check out the `after` branch for the final code. + +``` +$ git checkout after +``` diff --git a/getting-started/azure/main.tf b/getting-started/azure/main.tf new file mode 100644 index 0000000..9e312c4 --- /dev/null +++ b/getting-started/azure/main.tf @@ -0,0 +1,4 @@ +# Configure the provider +provider "azurerm" { + version = "=1.20.0" +} \ No newline at end of file diff --git a/getting-started/gcp/.gitignore b/getting-started/gcp/.gitignore new file mode 100644 index 0000000..4f2e2cf --- /dev/null +++ b/getting-started/gcp/.gitignore @@ -0,0 +1,3 @@ +terraform.tfvars +.terraform + diff --git a/getting-started/gcp/README.md b/getting-started/gcp/README.md new file mode 100644 index 0000000..ab7d68d --- /dev/null +++ b/getting-started/gcp/README.md @@ -0,0 +1,9 @@ +# Getting Started with Terraform on Google Cloud Platform + +This code accompanies the [Getting Started](https://learn.hashicorp.com/terraform/gcp/intro) guide on [learn.hashicorp.com](https://learn.hashicorp.com/). + +Check out the `after` branch for the final code: + +```shell +git checkout after +``` diff --git a/getting-started/gcp/main.tf b/getting-started/gcp/main.tf new file mode 100644 index 0000000..a31d4ae --- /dev/null +++ b/getting-started/gcp/main.tf @@ -0,0 +1,11 @@ +provider "google" { + credentials = file(".json") + + project = "" + region = "us-central1" + zone = "us-central1-c" +} + +resource "google_compute_network" "vpc_network" { + name = "terraform-network" +} diff --git a/getting-started/gcp/outputs.tf b/getting-started/gcp/outputs.tf new file mode 100644 index 0000000..d36d91b --- /dev/null +++ b/getting-started/gcp/outputs.tf @@ -0,0 +1 @@ +# Output definitions go here diff --git a/getting-started/gcp/terraform.tfvars.example b/getting-started/gcp/terraform.tfvars.example new file mode 100644 index 0000000..5be524d --- /dev/null +++ b/getting-started/gcp/terraform.tfvars.example @@ -0,0 +1 @@ +# Variable value assignments go here diff --git a/getting-started/gcp/variables.tf b/getting-started/gcp/variables.tf new file mode 100644 index 0000000..115a233 --- /dev/null +++ b/getting-started/gcp/variables.tf @@ -0,0 +1 @@ +# Variable definitions go here diff --git a/getting-started/gcp/versions.tf b/getting-started/gcp/versions.tf new file mode 100644 index 0000000..ac97c6a --- /dev/null +++ b/getting-started/gcp/versions.tf @@ -0,0 +1,4 @@ + +terraform { + required_version = ">= 0.12" +} diff --git a/getting-started/tfc/main.tf b/getting-started/tfc/main.tf new file mode 100644 index 0000000..851354b --- /dev/null +++ b/getting-started/tfc/main.tf @@ -0,0 +1,23 @@ +// terraform { +// backend "remote" { +// organization = "my-organization" +// workspaces { +// name = "random-pet-demo" +// } +// } +// } + +variable "stage" { + default = "production" +} + +resource "random_pet" "server" { + keepers = { + # Generate a new pet name each time we switch to a new stage + stage = "${var.stage}" + } +} + +output "random_server_id" { + value = random_pet.server.id +} diff --git a/main.tf b/main.tf index cb0ce0d..f7b347f 100644 --- a/main.tf +++ b/main.tf @@ -3,6 +3,19 @@ terraform { } provider "aws" { + # MODIFY this line to look for 2.27.0 or greater + version = ">= 2.22.0" +} + +data "aws_ami" "ubuntu_16_04" { + most_recent = true + + filter { + name = "name" + values = ["ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-*"] + } + + owners = ["099720109477"] } module "server" { @@ -10,7 +23,7 @@ module "server" { num_webs = var.num_webs identity = var.identity - ami = var.ami + ami = data.aws_ami.ubuntu_16_04.image_id ingress_cidr = var.ingress_cidr public_key = var.public_key private_key = var.private_key diff --git a/outputs.tf b/outputs.tf index 28aa9ee..7a3808f 100644 --- a/outputs.tf +++ b/outputs.tf @@ -5,4 +5,3 @@ output "public_ip" { output "public_dns" { value = module.server.public_dns } - diff --git a/server/main.tf b/server/main.tf index 09b8ca1..7e3ca5d 100644 --- a/server/main.tf +++ b/server/main.tf @@ -1,23 +1,17 @@ resource "aws_key_pair" "default" { key_name = "${var.identity}-key" - public_key = var.public_key + #public_key = var.public_key + public_key = file("~/.ssh/id_rsa.pub") } resource "aws_security_group" "default" { name_prefix = var.identity ingress { - from_port = 80 - to_port = 80 - protocol = "tcp" - cidr_blocks = [var.ingress_cidr] - } - - ingress { - from_port = 22 - to_port = 22 - protocol = "tcp" - cidr_blocks = [var.ingress_cidr] + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] } egress { @@ -50,8 +44,10 @@ resource "aws_instance" "web" { connection { type = "ssh" user = "ubuntu" - private_key = var.private_key + #private_key = var.private_key + private_key = file("~/.ssh/id_rsa") host = self.public_ip + timeout = "1m" } provisioner "file" { @@ -65,4 +61,3 @@ resource "aws_instance" "web" { ] } } - diff --git a/server/outputs.tf b/server/outputs.tf index 3032ae9..07fa9ed 100644 --- a/server/outputs.tf +++ b/server/outputs.tf @@ -5,4 +5,3 @@ output "public_ip" { output "public_dns" { value = [aws_instance.web.*.public_dns] } - diff --git a/server/variables.tf b/server/variables.tf index 3faea5e..78b793e 100644 --- a/server/variables.tf +++ b/server/variables.tf @@ -22,4 +22,3 @@ variable "public_key" { variable "private_key" { description = "Contents of the private key used to connect to this instance" } - diff --git a/variables.tf b/variables.tf index 3bfc90e..3a60e9b 100644 --- a/variables.tf +++ b/variables.tf @@ -2,26 +2,22 @@ variable "identity" { description = "A unique name for your resources" } -variable "ami" { - description = "The Amazon Machine Image for new instances." - default = "ami-c62eaabe" -} - -variable "ingress_cidr" { - default = "0.0.0.0/0" - description = "IP block from which connections to this instance will be made" -} - variable "public_key" { description = "Contents of the public key used to connect to this instance" + default = "" } variable "private_key" { description = "Contents of the private key used to connect to this instance" + default = "" +} + +variable "ingress_cidr" { + description = "IP block from which connections to this instance will be made" + default = "0.0.0.0/0" } variable "num_webs" { description = "The number of servers to run" default = "1" } -