https://github.com/swiftsoftwaregroup/aws-ec2-terraform
Deploy EC2 instance with Terraform
https://github.com/swiftsoftwaregroup/aws-ec2-terraform
Last synced: 4 months ago
JSON representation
Deploy EC2 instance with Terraform
- Host: GitHub
- URL: https://github.com/swiftsoftwaregroup/aws-ec2-terraform
- Owner: swiftsoftwaregroup
- License: apache-2.0
- Created: 2024-08-02T14:55:57.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-07T01:28:49.000Z (almost 2 years ago)
- Last Synced: 2025-02-24T11:49:43.672Z (over 1 year ago)
- Language: HCL
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# aws-ec2-terraform
Deploy EC2 instance with Terraform
## Setup for macOS
Make sure you do this setup first:
1. [Setup macOS for AWS Cloud DevOps](https://blog.swiftsoftwaregroup.com/setup-macos-for-aws-cloud-devops)
2. [AWS Authentication](https://blog.swiftsoftwaregroup.com/aws-authentication)
3. Install Terraform via Homebrew:
```bash
# install
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
# verify
terraform -help
```
## Development
Configure the project:
```bash
source configure.sh
```
Format configuration:
```bash
terraform fmt
```
Validate configuration:
```bash
terraform validate
```
### Deploy
Preview infrastructure:
```bash
terraform plan -out terraform.plan
```
Create infrastructure:
```bash
terraform apply terraform.plan
```
Check state:
```bash
terraform show
```
### Test Deployment
Check that you can browse the `nginx` default site:
```bash
instance_public_ip=$(terraform output -json | jq -r '.instance_public_ip.value')
open http://$instance_public_ip
```
Connect to instance via SSH:
```bash
key="aws-ec2-key"
instance_public_ip=$(terraform output -json | jq -r '.instance_public_ip.value')
ssh -i ~/.ssh/$key ec2-user@$instance_public_ip
```
## Cleanup
Review the changes and verify the execution plan:
```bash
terraform plan -destroy
```
Delete the resources:
```bash
terraform destroy
```
## How to create a new project
```bash
# create main.tf
cat << 'EOF' > main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
# AWS config
# leave empty to read the settings from the AWS CLI configuration
provider "aws" {}
EOF
# initialize the project
# this will download provider plugin into .terraform subdir
terraform init
```