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

[Firefly] Composed by AI: instance.tf #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,27 @@ For more Community Modules not listed here please see the [Terraform Module Regi
[![CC0](http://mirrors.creativecommons.org/presskit/buttons/88x31/svg/cc-zero.svg)](https://creativecommons.org/publicdomain/zero/1.0/)

To the extent possible under law, Shuaib Yunus has waived all copyright and related or neighboring rights to this work.

# Creating an AWS EC2 Instance with Two Disks

This Terraform script creates an AWS EC2 instance with two disks: a root volume and a data volume. The instance type and key pair name are specified in `variables.tf`. The security groups for SSH and HTTP access are defined in `security_groups.tf`.

The `userdata.sh` file initializes the instance upon launch. In this example, it simply adds a "Hello World!" message to a file.

To create the instance, run the following commands:

```
terraform init
terraform apply
```

After the instance is created, you can SSH into it using the key pair specified in `variables.tf`.

Example output:

```
Outputs:

instance_ip = "X.X.X.X"
```
This code was generated by [Firefly](https://app.gofirefly.io)
36 changes: 36 additions & 0 deletions instance.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
resource "aws_instance" "instance1" {
ami = data.aws_ami.amzn_linux.id
instance_type = var.instance_type
key_name = var.key_name
security_groups = [
aws_security_group.ssh_access.id,
aws_security_group.http_access.id,
]

tags = {
Name = "Instance1"
}

userData = filebase64("userdata.sh")

root_block_device {
volume_size = var.root_volume_size
volume_type = var.root_volume_type
}

ebs_block_device {
device_name = "/dev/sdb"
volume_size = var.data_volume_size
volume_type = var.data_volume_type
}
}

data "aws_ami" "amzn_linux" {
most_recent = true
owners = ["amazon"]

filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
25 changes: 25 additions & 0 deletions security_groups.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
resource "aws_security_group" "ssh_access" {
name = "ssh_access"

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

resource "aws_security_group" "http_access" {
name = "http_access"

ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [
"0.0.0.0/0"
]
}
}
3 changes: 3 additions & 0 deletions userdata.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "Hello World!" >> /home/ec2-user/hello.txt
23 changes: 23 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
variable "instance_type" {
default = "t2.micro"
}

variable "key_name" {
default = "my-key-pair"
}

variable "root_volume_size" {
default = 8
}

variable "root_volume_type" {
default = "gp2"
}

variable "data_volume_size" {
default = 10
}

variable "data_volume_type" {
default = "gp2"
}