##困っっていること・前提
EC2に接続しMySQLに接続したいのですが接続ができません。
##エラー
$mysql -u admin -p -h ☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓☓ >ERROR 2003 (HY000): Can't connect to MySQL server on '☓☓☓' (110)
##コード
sg.tf
resource "aws_security_group" "sample_sg" { vpc_id = aws_vpc.sample_vpc.id name = "sample_sg" description = "Define of SG for public" egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } tags = { Name = "sample_sg" } } #セキュリティグループルール resource "aws_security_group_rule" "inbound_http" { type = "ingress" from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] # source_security_group_id = aws_security_group.sample_sg.id security_group_id = aws_security_group.sample_sg.id } resource "aws_security_group_rule" "inbound_ssh" { type = "ingress" from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] security_group_id = aws_security_group.sample_sg.id }
rds.tf
resource "aws_db_instance" "db" { identifier = "sample-db" allocated_storage = 20 storage_type = "gp2" engine = "mysql" engine_version = "8.0.21" instance_class = "db.t2.micro" multi_az = true name = "testdb" username = "admin" password = "admintest" final_snapshot_identifier = "aws-sample-db" db_subnet_group_name = aws_db_subnet_group.sample_subnet_group_db.name vpc_security_group_ids = [aws_security_group.sample_sg.id] parameter_group_name = aws_db_parameter_group.default.name } resource "aws_db_subnet_group" "sample_subnet_group_db" { name = "sample_db" subnet_ids = [aws_subnet.sample_private_1a.id, aws_subnet.sample_private_1c.id] description = "It is a DB subnet group on sample_vpc." tags = { Name = "sample_sg_db" } } #セキュリティグループの設定 resource "aws_security_group" "sample_sg_db" { vpc_id = aws_vpc.sample_vpc.id name = "db" description = "Allow DB inbound traffic" ingress { from_port = 3306 to_port = 3306 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] # security_groups = [aws_security_group.sample_sg.id] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } tags = { Name = "sample_allow_db" } } #データーベースのコンフィグの管理 resource "aws_db_parameter_group" "default" { name = "rds-pg" family = "mysql8.0" } output "db_endpoint" { value = aws_db_instance.db.endpoint } output "db_name" { value = aws_db_instance.db.name } output "db_user" { value = aws_db_instance.db.username } output "db_password" { value = aws_db_instance.db.password }
vpc.tf
resource "aws_vpc" "sample_vpc" { cidr_block = "10.0.0.0/16" enable_dns_hostnames = true enable_dns_support = true tags = { # type = string Name = "sample_vpc" } } #パブリックサブネット resource "aws_subnet" "sample_public_1a" { vpc_id = aws_vpc.sample_vpc.id cidr_block = "10.0.0.0/24" availability_zone = "ap-northeast-1a" map_public_ip_on_launch = true tags = { # type = string Name = "public_subnet_1a" } timeouts { delete = "5m" } } #プライベートサブネット resource "aws_subnet" "sample_private_1a" { vpc_id = aws_vpc.sample_vpc.id cidr_block = "10.0.1.0/24" availability_zone = "ap-northeast-1a" # map_public_ip_on_launch = true tags = { Name = "private_subnet_1a" } } resource "aws_subnet" "sample_private_1c" { vpc_id = aws_vpc.sample_vpc.id cidr_block = "10.0.2.0/24" availability_zone = "ap-northeast-1c" tags = { Name = "private_subnet_1c" } } #インターネットゲートウェイ resource "aws_internet_gateway" "sample_igw" { vpc_id = aws_vpc.sample_vpc.id tags = { Name = "sample_igw" } } #ルートテーブルの定義 resource "aws_route_table" "sample_public" { vpc_id = aws_vpc.sample_vpc.id route { cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.sample_igw.id } tags = { Name = "sample_route_table" } } #ルートテーブルのサブネットの紐付け resource "aws_route_table_association" "public_1a" { subnet_id = aws_subnet.sample_public_1a.id route_table_id = aws_route_table.sample_public.id }
回答1件
あなたの回答
tips
プレビュー