From 1bb150d44e2d287591a96d6949004b3be99fdea7 Mon Sep 17 00:00:00 2001 From: Daniel Mason Date: Sat, 22 Apr 2023 12:37:30 +1200 Subject: [PATCH] Initial Commit --- .github/workflows/your-fork.yml | 16 ------- data.tf | 23 +++++++++ ec2.tf | 35 ++++++++++++++ main.tf | 82 --------------------------------- output.tf | 0 provider.tf | 27 +++++++++++ templates/userdata.tpl | 2 + 7 files changed, 87 insertions(+), 98 deletions(-) delete mode 100644 .github/workflows/your-fork.yml create mode 100644 data.tf create mode 100644 ec2.tf create mode 100644 output.tf create mode 100644 provider.tf create mode 100644 templates/userdata.tpl diff --git a/.github/workflows/your-fork.yml b/.github/workflows/your-fork.yml deleted file mode 100644 index f5b4694..0000000 --- a/.github/workflows/your-fork.yml +++ /dev/null @@ -1,16 +0,0 @@ -name: Your Fork - -on: - pull_request_target: - types: [opened] - -jobs: - close: - if: github.repository == 'hashicorp/learn-terraform-github-actions' - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - # Optional. Post a issue comment just before closing a pull request. - comment: "Hi! If you are following the Terraform GitHub Actions tutorial, please open the PR against [your personal fork](https://learn.hashicorp.com/tutorials/terraform/github-actions?in=terraform/automation#set-up-a-github-repository) of this repository. We will automatically close this PR, but if you intended to edit the example itself please feel free to re-open it." diff --git a/data.tf b/data.tf new file mode 100644 index 0000000..e48126e --- /dev/null +++ b/data.tf @@ -0,0 +1,23 @@ +# Get current AWS Region +data "aws_region" "current" {} + +# Get available AZs +data "aws_availability_zones" "available" {} + +# Get latest official Ubuntu AMI +data "aws_ami" "ubuntu" { + most_recent = true + + filter { + name = "name" + values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-arm64-server-*"] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + + owners = ["099720109477"] # Canonical +} + diff --git a/ec2.tf b/ec2.tf new file mode 100644 index 0000000..e21c15a --- /dev/null +++ b/ec2.tf @@ -0,0 +1,35 @@ + +# # Instance +# resource "aws_instance" "instance" { +# ami = data.aws_ami.ubuntu.id +# instance_type = "t4g.micro" +# iam_instance_profile = aws_iam_instance_profile.profile.name +# availability_zone = element(aws_subnet.subnet.*.availability_zone, 1) +# user_data = data.template_file.userdata.rendered +# subnet_id = element(aws_subnet.subnet.*.id, 1) +# key_name = var.ssh_key +# vpc_security_group_ids = [aws_security_group.sg.id] +# } + +# # Elastic IP +# resource "aws_eip" "eip" { +# instance = aws_instance.instance.id +# vpc = true + +# tags = var.tags +# } + + +# # Create a new load balancer attachment +# resource "aws_elb_attachment" "attachment" { +# elb = aws_elb.lb.id +# instance = aws_instance.instance.id +# } + +# # EBS Vol for persistance +# resource "aws_ebs_volume" "instance" { +# availability_zone = element(aws_subnet.subnet.*.availability_zone, 1) +# size = "8" +# type = "gp2" +# encrypted = true +# } diff --git a/main.tf b/main.tf index 921bcf7..e69de29 100644 --- a/main.tf +++ b/main.tf @@ -1,82 +0,0 @@ -# Copyright (c) HashiCorp, Inc. -# SPDX-License-Identifier: MPL-2.0 - -terraform { - required_providers { - aws = { - source = "hashicorp/aws" - version = "4.52.0" - } - random = { - source = "hashicorp/random" - version = "3.4.3" - } - } - required_version = ">= 1.1.0" - - cloud { - organization = "REPLACE_ME" - - workspaces { - name = "learn-terraform-github-actions" - } - } -} - -provider "aws" { - region = "us-west-2" -} - -resource "random_pet" "sg" {} - -data "aws_ami" "ubuntu" { - most_recent = true - - filter { - name = "name" - values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] - } - - filter { - name = "virtualization-type" - values = ["hvm"] - } - - owners = ["099720109477"] # Canonical -} - -resource "aws_instance" "web" { - ami = data.aws_ami.ubuntu.id - instance_type = "t2.micro" - vpc_security_group_ids = [aws_security_group.web-sg.id] - - user_data = <<-EOF - #!/bin/bash - apt-get update - apt-get install -y apache2 - sed -i -e 's/80/8080/' /etc/apache2/ports.conf - echo "Hello World" > /var/www/html/index.html - systemctl restart apache2 - EOF -} - -resource "aws_security_group" "web-sg" { - name = "${random_pet.sg.id}-sg" - ingress { - from_port = 8080 - to_port = 8080 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - // connectivity to ubuntu mirrors is required to run `apt-get update` and `apt-get install apache2` - egress { - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - } -} - -output "web-address" { - value = "${aws_instance.web.public_dns}:8080" -} diff --git a/output.tf b/output.tf new file mode 100644 index 0000000..e69de29 diff --git a/provider.tf b/provider.tf new file mode 100644 index 0000000..229b420 --- /dev/null +++ b/provider.tf @@ -0,0 +1,27 @@ +# TF state +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "4.52.0" + } + random = { + source = "hashicorp/random" + version = "3.4.3" + } + } + required_version = ">= 1.1.0" + + cloud { + organization = "fediservices" + + workspaces { + name = "aws-infra" + } + } +} + +# AWS Provider +provider "aws" { + region = "ap-southeast-2" +} \ No newline at end of file diff --git a/templates/userdata.tpl b/templates/userdata.tpl new file mode 100644 index 0000000..05a7907 --- /dev/null +++ b/templates/userdata.tpl @@ -0,0 +1,2 @@ +#!/bin/bash +