https://github.com/remiljw/learn-terraform-aws-instance
Infrastructure as Code to create an S3 bucket and EC2 instance with python web server
https://github.com/remiljw/learn-terraform-aws-instance
amazon-web-services aws iac infrastructure-as-code python terraform webserver
Last synced: 3 months ago
JSON representation
Infrastructure as Code to create an S3 bucket and EC2 instance with python web server
- Host: GitHub
- URL: https://github.com/remiljw/learn-terraform-aws-instance
- Owner: remiljw
- License: mit
- Created: 2021-09-24T21:45:18.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-01-29T16:53:58.000Z (over 1 year ago)
- Last Synced: 2025-01-20T12:23:35.281Z (5 months ago)
- Topics: amazon-web-services, aws, iac, infrastructure-as-code, python, terraform, webserver
- Language: HCL
- Homepage:
- Size: 65.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/remiljw/learn-terraform-aws-instance/actions/workflows/test.yml) [](https://github.com/remiljw/learn-terraform-aws-instance/actions/workflows/deploy.yml)
# Prerequisites
To be able to run or test this infrastructure, you will need to get a few things up and running:
- [Golang](https://golang.org/doc/install) installed
- A [Terraform Cloud Account](https://app.terraform.io/)
- The [Terraform CLI](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started) 0.14.8+ installed.
- The [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed.
- An [AWS](https://aws.amazon.com/free/) account.
- Your AWS credentials. You can create a new [Access Key on this page](https://console.aws.amazon.com/iam/home?#/security_credentials).# Steps
- Launch your terminal
- Run `aws configure` to configure the AWS CLI from your terminal. Follow the prompts to input your AWS Access Key ID and Secret Access Key. Set the region `us-east-1`, then `json` as format.- Open `server.py` file and on line 3, fill in the `aws_access_key_id` `aws_secret_access_key` fields with your acquired AWS credentials from the previous step.
```python
ec2 = boto3.client('ec2', aws_access_key_id='', aws_secret_access_key='', region_name=region)
```
- You will need to create your organization and workspace on Terraform Cloud. Follow these [instructions](https://learn.hashicorp.com/tutorials/terraform/github-actions) to help you set that up.- Open `main.tf` and on line 5 and 9 respectively, replace the `organization` and workspaces `name` field to the organization and workspace you created in the previous step.
```hcl
terraform {
backend "remote" {
# # The name of your Terraform Cloud organization.
hostname = "app.terraform.io"
organization = ""
#
# # The name of the Terraform Cloud workspace to store Terraform state files in.
workspaces {
name = ""
}
}
```
- Create your [AWS Key Pair in the AWS Console](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html#having-ec2-create-your-key-pair), download it, and move it to the root of this project.- Open `main.tf` and on line 131 change the value of `key_name` to the name of your downloaded key
```json
key_name = ""
```
- On line 146, edit the `private_key` to the path of tour key which you added to the root of this project.
```json
private_key = file("${path.module}/.pem")
```- Launch your terminal and cd to the root of this project, run the following commands to deploy the infrastructure:
- - `terraform init`
- - - initiates terraform
- - `terraform fmt`
- - - formats the files to match the canonical standard
- - `terraform validate`
- - - ensures the files are syntactically valid and internally consistent
- - `terraform apply -auto-approve`
- - - creates the infrastructure- Wait for the infrastructure to deploy. On successful deploy you should have an output as this on your terminal:
```shell
Apply complete! Resources: 10 added, 0 changed, 0 destroyed.Outputs:
instance_id = "i-0941f1d034a2f2f6d"
instance_url = "Visit http://52.204.187.105:8080 in your browser."
public_ip = "52.204.187.105"
resource_tags = tomap({
"Name" = "Flugel"
"Owner" = "InfraTeam"
})
```
- Run `terraform show` to see the details of the infrastructure.
- Once you are done, you will need to destroy what you just built, so run:
`terraform destroy -auto-approve`# Testing
To run the tests:
- Launch your terminal and cd to the root of this project
- run `cd test` to navigate to the test folder
- run `go build -v`
- run `go test -v`