CMDB-Server/CMDB/ec2.bak

106 lines
3.0 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ---------------------------
# EC2 Keypairの設定
# ---------------------------
# 鍵の名前を指定
variable "key_name" {
default = "cmdb-keypair"
}
# 秘密鍵のアルゴリズム設定
resource "tls_private_key" "cmdb_private_key" {
algorithm = "RSA"
rsa_bits = 2048
}
# 生成するkeypair秘密鍵と公開鍵のパスを指定
locals {
public_key_file = "./.keypair/${var.key_name}.id_rsa.pub"
private_key_file = "./.keypair/${var.key_name}.id_rsa"
}
# 鍵の生成
resource "local_file" "cmdb_private_key_pem" {
filename = local.private_key_file
content = tls_private_key.cmdb_private_key.private_key_pem
}
# 公開鍵をAWSのkeypairにインポート
resource "aws_key_pair" "cmdb_keypair" {
key_name = var.key_name
public_key = tls_private_key.cmdb_private_key.public_key_openssh
}
# ---------------------------
# EC2 インスタンス
# ---------------------------
# Amazon Linux2のAMIを取得
data "aws_ssm_parameter" "amazon_linux2023" {
name = "/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-6.1-x86_64"
}
# ネットワークインターフェイスの作成
resource "aws_network_interface" "cmdb_ni_web1" {
subnet_id = aws_subnet.cmdb_public_1a_subnet.id
private_ips = ["10.5.1.10"]
# セキュリティグループの指定
security_groups = [aws_security_group.cmdb_ec2_sg.id]
tags = {
Name = "cmdb-ni-web1"
}
}
resource "aws_network_interface" "cmdb_ni_web2" {
subnet_id = aws_subnet.cmdb_public_1c_subnet.id
private_ips = ["10.5.3.10"]
# セキュリティグループの指定
security_groups = [aws_security_group.cmdb_ec2_sg.id]
tags = {
Name = "cmdb-ni-web2"
}
}
# EC2インスタンスの作成
resource "aws_instance" "cmdb_ec2" {
# AMIの指定
ami = data.aws_ssm_parameter.amazon_linux2023.value
# インスタンスタイプの指定
instance_type = "t3a.medium"
# EBSのルートボリューム設定
root_block_device {
# ボリュームサイズ(GiB)
volume_size = 64
# ボリュームタイプ
volume_type = "gp3"
# GP3のIOPS
iops = 3000
# GP3のスループット
throughput = 125
# EC2終了時に削除
delete_on_termination = true
# EBSのNameタグ
tags = {
Name = "gp3-dev-ec2"
}
}
# アベイラビリティーゾーンの指定
availability_zone = "ap-northeast-1a"
# サブネットの指定 : NICの設定と重複するため不要
# subnet_id = aws_subnet.cmdb_public_1a_subnet.id
# ネットワークインターフェイスの指定
network_interface {
network_interface_id = aws_network_interface.cmdb_ni_web1.id
device_index = 0
}
# キーペアの指定
key_name = var.key_name
# インスタンスプロファイルの指定
iam_instance_profile = aws_iam_instance_profile.cmdb_ssm_instance_profile.name
# user dataの指定CMDBのインストール
user_data = file("install.sh")
tags = {
Name = "cmdb-ec2"
}
}