[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

[Must-see for beginners] ChatGPT wrote Terraform code

This is Ken, who has caught a cold for the first time in five years, and is responding quite well.

It's been about a year since I started using AWS services.

I thought it would be nice to start up and configure EC2 instances etc. on the console, but I think it is gradually becoming more common to implement infrastructure in code. Following this trend, I myself decided to try launching AWS resources with Terraform. However, everything takes time at first.

So, this time I created Terraform code together with ChatGPT and modified it.

Environmental information

WSL2

windows 11

Terraform v1.2.5

The AWS configuration we built this time

 

Prompt words I tried

The prompt input I tried this time is extremely simple.

I would like to build on AWS as shown below.

VPC 1

Route table 1

Public subnet 2

Private subnet 2

EC2 1

ALB 1

RDS 1

If you enter a prompt like this, ChatGPT will write various things.

If this continues, there may not be enough Target Groups or RDS Subnet Groups, so we will ask you to create additional locations.

If you want to continue basic conversation,

Add Target Group to the above code

It seems that if you add a keyword such as ``above'', the name will be inherited.

If they don't take over, they will write the code using that name even if they don't want to. Also, when I'm curious, I use Google search to find out if it's something like this.

tf file written with help from ChatGPT

This time, in order to make it easier to understand, I have combined the files into one file and created it without using variables as much as possible. I think it is also possible to create a tf file using variables etc. using ChatGPT, so if you would like to create it in that format, please give it a try.

# Configure provider provider "aws" { region = "ap-northeast-1" } # Create VPC resource "aws_vpc" "main" { cidr_block = "10.0.0.0/16" } # Create internet gateway resource "aws_internet_gateway" "gw" { vpc_id = aws_vpc.main.id } # Create public subnet 1 resource "aws_subnet" "public_subnet1" { vpc_id = aws_vpc.main.id cidr_block = "10.0.1.0/24" availability_zone = "ap-northeast-1a " map_public_ip_on_launch = true } # Create public subnet 2 resource "aws_subnet" "public_subnet2" { vpc_id = aws_vpc.main.id cidr_block = "10.0.2.0/24" availability_zone = "ap-northeast-1c" map_public_ip_on_launch = true } # Private Create subnet 1 resource "aws_subnet" "private_subnet1" { vpc_id = aws_vpc.main.id cidr_block = "10.0.3.0/24" availability_zone = "ap-northeast-1a" } # Create private subnet 2 resource "aws_subnet" "private_subnet2 " { vpc_id = aws_vpc.main.id cidr_block = "10.0.4.0/24" availability_zone = "ap-northeast-1c" } # Create route table resource "aws_route_table" "public_route_table" { vpc_id = aws_vpc.main.id route { cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.gw.id } } # Associate a route table with a public subnet resource "aws_route_table_association" "public_subnet1_association" { subnet_id = aws_subnet.public_subnet1.id route_table_id = aws_route_table.public_route_table.id } resource "aws_route_table_association" "public_subnet2_association" { subnet_id = aws_subnet.public_subnet2.id route_table_id = aws_route_table.public_route_table.id } # Create ALB resource "aws_lb" "alb" { name = "test-alb" subnets = [aws_subnet.public_subnet1.id, aws_subnet.public_subnet2.id] internal = false load_balancer_type = "application" } # Create EC2 resource "aws_instance" "ec2" { ami = "ami-03dceaabddff8067e" instance_type = "t2.micro" subnet_id = aws_subnet.public_subnet1.id key_name = "hogehoge" } # Create a target group resource "aws_lb_target_group" "target_group" { name = "my-target-group" port = 80 protocol = "HTTP" vpc_id = aws_vpc.main.id target_type = "instance" } # ALB and Target group association resource "aws_lb_listener" "listener" { load_balancer_arn = aws_lb.alb.arn port = 80 protocol = "HTTP" default_action { type = "forward" target_group_arn = aws_lb_target_group.target_group.arn } } # Register instance to target group resource "aws_lb_target_group_attachment" "target_group_attachment" { target_group_arn = aws_lb_target_group.target_group.arn target_id = aws_instance.ec2.id port = 80 } # Create RDS resource "aws_db_instance" "rds" { engine = "mysql" instance_class = "db.t2. micro" allocated_storage = 10 engine_version = "5.7" identifier = "my-rds-instance" username = "admin" password = "hogehoge" skip_final_snapshot = true publicly_accessible = false vpc_security_group_ids = [aws_security_group.rds_security_group.id] db_subnet_group_name = aws_db_subnet_group.db_subnet_group. name } # Create a security group to allow access to RDS resource "aws_security_group" "rds_security_group" { vpc_id = aws_vpc.main.id ingress { from_port = 3306 to_port = 3306 protocol = "tcp" cidr_blocks = ["0.0.0.0/ 0"] } } # Create a DB subnet group resource "aws_db_subnet_group" "db_subnet_group" { name = "my-db-subnet-group" description = "My DB Subnet Group" subnet_ids = [aws_subnet.private_subnet1.id, aws_subnet.private_subnet2 .id] }

summary

Beginners have the disadvantage of not knowing which code is correct, but I think there is a way to try out numbers first and gradually understand that it is easier to write or see. That's what I thought about today.

Also, the code generated by ChatGPT is not perfect, so in this case, I think an error will occur when you run terraform apply. When this happens, you will definitely need to make corrections yourself.

However, without thinking that it's a hassle, as you investigate what's wrong, you'll gradually realize things like, ``I see, this is the meaning of the error!Then, this is the wrong way to write it.'' I think it is.

So, don't give up even if you get an error, try again and again and enjoy the rich life of an engineer.

If you found this article helpful , please give it a like!
6
Loading...
6 votes, average: 1.00 / 16
1,304
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

The person who wrote this article

About the author

Ken

Joined Beyond as a new graduate in 2022.Currently works
in the System Solutions Department.I
studied linguistics until graduate school and took on the challenge of becoming an infrastructure engineer.I
established an in-house club called the Beyond Cafe Club, and am passionate about making coffee and tea every day. ing