Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Te makere patch 1 #66

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c1d01d3
Code after end of class
topfunky Dec 2, 2017
aae9fd8
Add variable declarations for all values
topfunky Dec 2, 2017
a6ba40e
Default to creating one instance
topfunky Dec 4, 2017
c52e9e6
Bundle Go app and deploy with terraform
topfunky Dec 4, 2017
ad5e892
Remove echo module
topfunky Dec 4, 2017
ccb623a
Fix whitespace and trailing comma
topfunky Dec 4, 2017
1e705ca
Refactor to a module
topfunky Dec 4, 2017
3ef4e52
Set version for AWS provider
topfunky Dec 5, 2017
87f612b
Revert to `num_webs` instead of `total_webs`
topfunky Dec 6, 2017
b6cc88e
Refactor variable names to match lab
topfunky Dec 6, 2017
65309e1
Simplify server setup with a script
topfunky Dec 7, 2017
4bcfac4
Rename variables to match attribute names
topfunky Dec 7, 2017
967c69e
Fix path to script
topfunky Dec 7, 2017
586713b
Cleanup unused code. Remove redundant default.
topfunky Dec 13, 2017
f05db9e
Merge from `master`
topfunky Dec 19, 2017
218e54b
Tweaks for use on a student's own AWS account
topfunky Jan 24, 2018
64b7e5b
Ensure that webapp starts on reboot
topfunky Jan 25, 2018
0ca74da
Tweak for use without SSH
topfunky Jan 25, 2018
a0300e2
Remove unnecessary code now that an AMI is used
topfunky Jan 26, 2018
70cba1c
Variables for resources to satify security requirements
topfunky Jan 26, 2018
95a8dd5
Refactor to individual files. Make independent of other AWS infra.
topfunky Feb 15, 2018
b4fb90f
Notes on the code in this branch
topfunky Feb 15, 2018
0c1e7db
Refactor to work with Terraform Enterprise
topfunky Jun 2, 2018
401682e
Notes about requirements for running this branch
topfunky Jun 2, 2018
7b903a0
0.12 updates
Jul 2, 2019
196ecca
Update readme to TF Cloud
Jul 2, 2019
70a1b00
Update main.tf
Jul 2, 2019
2584116
required version
Jul 7, 2019
7456d7b
Updating with 201 content
Sep 6, 2019
338f3ef
Revert "Updating with 201 content"
Sep 6, 2019
546cde8
Updating with 201 code
Sep 6, 2019
062e0b0
Update variables.tf
Sep 6, 2019
bf64f1e
Updating workspace names
Sep 6, 2019
d7d1ade
Merge pull request #33 from hashicorp/res-after-tfc
Sep 6, 2019
165c035
Update main.tf
TeMakere Dec 4, 2020
7dde4fe
Update main.tf
TeMakere Dec 4, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updating with 201 content
  • Loading branch information
Rachel committed Sep 6, 2019
commit 7456d7b6e33001c8cb0c90561a72daf658d4463d
4 changes: 4 additions & 0 deletions 01_read_state/primary/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# primary/main.tf
output "public_ip" {
value = "8.8.8.8"
}
12 changes: 12 additions & 0 deletions 01_read_state/secondary/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# secondary/main.tf
# Read state from another Terraform config’s state
data "terraform_remote_state" "primary" {
backend = "local"
config = {
path = "../primary/terraform.tfstate"
}
}

output "primary_public_ip" {
value = data.terraform_remote_state.primary.outputs.public_ip
}
29 changes: 29 additions & 0 deletions 02_store_state/read_state/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
terraform {
backend "remote" {
organization = "<ORGANIZATION NAME>"

workspaces {
name = "terraform_cloud_read_state"
}
}
}

resource "random_id" "random" {
keepers = {
uuid = uuid()
}

byte_length = 8
}

data "terraform_remote_state" "write_state" {
backend = "remote"

config = {
organization = "<ORGANIZATION NAME>"

workspaces = {
name = "terraform_cloud_write_state"
}
}
}
23 changes: 23 additions & 0 deletions 02_store_state/write_state/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# lab_2_terraform_cloud_demo/write_state/main.tf
terraform {
backend "remote" {
organization = "<ORGANIZATION NAME>"

workspaces {
name = "terraform_cloud_write_state"
}
}
}


resource "random_id" "random" {
keepers = {
uuid = uuid()
}

byte_length = 8
}

output "random" {
value = random_id.random.hex
}
33 changes: 33 additions & 0 deletions 04_template_file/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
variable "owner_id" {
default = "anaconda"
}

locals {
policy = templatefile("${path.module}/templates/iam_policy.json.tpl", {
owner_id = var.owner_id
bucket_name = "${var.owner_id}-${uuid()}"
})
}


provider "aws" {
}

resource "aws_s3_bucket" "bucket1" {
bucket = "${var.owner_id}-${uuid()}"
acl = "private"
}

resource "aws_iam_policy" "bucket1"{
name = "${aws_s3_bucket.bucket1.id}-policy"
policy = local.policy
}

resource "aws_iam_user_policy_attachment" "attach-policy" {
user = var.owner_id
policy_arn = aws_iam_policy.bucket1.arn
}

output "iam_policy" {
value = local.policy
}
18 changes: 18 additions & 0 deletions 04_template_file/templates/iam_policy.json.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"Id": "Policy1527877254663",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1527877245190",
"Action": [
"s3:CreateBucket",
"s3:DeleteBucket",
"s3:DeleteObject",
"s3:GetObject",
"s3:ListObjects"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::${bucket_name}"
}
]
}
89 changes: 89 additions & 0 deletions 05_multi-provider/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
variable "github_token" {
}

variable "ami" {
}

variable "identity" {
default = "anaconda"
}

variable "namespace" {
default = "multi-provider-demo"
}

provider "github" {
token = var.github_token
organization = "placeholder"
}

provider "aws" {
version = ">= 1.19.0"
}

data "github_ip_ranges" "test" {
}

resource "aws_security_group" "training" {
name_prefix = var.namespace

ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"

cidr_blocks = ["0.0.0.0/0"]
#cidr_blocks = data.github_ip_ranges.test.pages
}
}

resource "aws_key_pair" "training" {
key_name = "${var.identity}-${var.namespace}-key"
public_key = file("~/.ssh/id_rsa.pub")
}

resource "aws_instance" "example" {
ami = var.ami
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.training.id]

key_name = aws_key_pair.training.id

tags = {
Name = "${var.identity}-simple-instance"
}

connection {
type = "ssh"
user = "ubuntu"
private_key = file("~/.ssh/id_rsa")
host = aws_instance.example.public_ip
}

provisioner "remote-exec" {
inline = [
"ping -c 5 ${cidrhost(element(data.github_ip_ranges.test.pages, 0), 0)}",
"ping -c 5 hashicorp.com",
]
}
}

output "github_pages_ip_ranges" {
value = data.github_ip_ranges.test.pages
}

output "public_ip" {
value = [aws_instance.example.*.public_ip]
}

output "public_dns" {
value = [aws_instance.example.*.public_dns]
}

34 changes: 34 additions & 0 deletions 06_lifecycles/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
provider "aws" {}

resource "aws_security_group" "training" {
name_prefix = "demo"
#name_prefix = "demo-modified"
ingress {
from_port = 0
to_port = 0
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

# lifecycle {
# create_before_destroy = true
# prevent_destroy = true
# }
}

resource "aws_instance" "web" {
ami = "ami-0735ea082a1534cac"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.training.id]

tags = {
name = "demo-simple-instance"
}
}
7 changes: 7 additions & 0 deletions 07_sentinel/sentinel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"mock": {
"tfconfig": "testdata/mock-tfconfig.sentinel",
"tfplan": "testdata/mock-tfplan.sentinel",
"tfstate": "testdata/mock-tfstate.sentinel"
}
}
28 changes: 28 additions & 0 deletions 07_sentinel/sentinel_demo/instance_type_is_medium.sentinel
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import "tfplan"

allowed_sizes = ["t2.medium"]

# Get all AWS instances contained in all modules being used
get_aws_instances = func() {
instances = []
for tfplan.module_paths as path {
instances += values(tfplan.module(path).resources.aws_instance) else []
}
return instances
}

aws_instances = get_aws_instances()

instance_types = rule {
all aws_instances as _, instances {
all instances as index, r {
all allowed_sizes as t {
r.applied.instance_type contains t
}
}
}
}

main = rule {
(instance_types) else true
}
3 changes: 3 additions & 0 deletions 07_sentinel/simple/simple.sentinel
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
main = rule {
true
}
5 changes: 5 additions & 0 deletions 07_sentinel/simple/test/simple/pass.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"test": {
"main": true
}
}
30 changes: 30 additions & 0 deletions 07_sentinel/with-data/test/with-data/expected-failure.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"global": {
"instance_type": "t2.nano",
"ami": {
"id": "ami-11111111"
}
},
"mock": {
"tfplan": {
"random_pet": {
"server": {
"0": {
"applied": {
"id": "deciding-pegasus",
"length": "2",
"separator": "-"
},
"diff": {}
}
}
}
}
},
"test": {
"main": false,
"instance_type_is_medium": false,
"ami_is_present": false,
"has_id": false
}
}
29 changes: 29 additions & 0 deletions 07_sentinel/with-data/test/with-data/pass.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"global": {
"instance_type": "t2.medium",
"ami": {
"id": "ami-e474db9c"
}
},
"mock": {
"tfplan": {
"random_pet": {
"server": {
"0": {
"applied": {
"id": "deciding-pegasus",
"length": "2",
"separator": "-"
},
"diff": {}
}
}
}
}
},
"test": {
"main": true,
"instance_type_is_medium": true

}
}
21 changes: 21 additions & 0 deletions 07_sentinel/with-data/with-data.sentinel
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import "tfplan"

main = rule {
instance_type_is_medium and
ami_is_present and
has_id
}

instance_type_is_medium = rule {
instance_type is "t2.medium"
}

ami_is_present = rule {
ami.id is "ami-e474db9c"
}

has_id = rule {
any tfplan.random_pet.server as _, servers {
servers.applied.id is "deciding-pegasus"
}
}
Loading