【问题标题】:Creating terraform aws rds first then create ec2 instance首先创建 terraform aws rds 然后创建 ec2 实例
【发布时间】:2021-10-11 05:28:23
【问题描述】:

我想使用 terraform 创建一个 rds 和 ec2 实例,我可以做到,但问题是我现在想使用模板数据源脚本在 ec2 bash 脚本中使用 rds 端点发生了什么是我的 ec2首先创建实例,然后创建我的 rds,因为我无法使用 rds 端点,因为它是在当时创建的。所以一旦它创建了,我的 ec2 数据源脚本就应该运行。


# RDS 
resource "aws_db_parameter_group" "terraform_postgres_parameters" {
  name        = format("%s-%s-%s", var.PROJECT_NAME, var.RDS_IDENTIFIER , "parameters")
  family      = format("%s%s", var.ENGINE, var.ENGINE_VERSION)
  description = format("%s %s %s", var.PROJECT_NAME, var.ENGINE , "parameter group")

}

resource "aws_db_instance" "terraform_postgres" {
  allocated_storage       = 20               # min_storage
  max_allocated_storage   = 100              # max_storage
  engine                  = var.ENGINE
  engine_version          = var.ENGINE_VERSION
  instance_class          = "db.t2.micro"
  identifier              = var.RDS_IDENTIFIER
  name                    = var.RDS_NAME
  username                = var.RDS_USERNAME
  password                = var.RDS_PASSWORD
  parameter_group_name    = aws_db_parameter_group.terraform_postgres_parameters.name
  multi_az                = "false"
  storage_type            = "gp2"
  backup_retention_period = 30
  skip_final_snapshot     = true
  publicly_accessible     = true
  tags = {
    Name = "terraform_postgres_instance"
  }
}


# EC2 Instance
resource "aws_instance" "example" {
  ami           = "ami-0bdef2eb518663879"
  instance_type = "t2.micro"

  user_data = data.template_cloudinit_config.cloudinit-example.rendered
}

# Templete File
data "template_file" "shell-script" {
  template = file("script/cloud-init.sh")
  vars = {
    DB_HOST = aws_db_instance.terraform_postgres.endpoint   # Need to use rds endpoint here
    DB_USER = var.RDS_USERNAME
    DB_PASSWORD = var.RDS_PASSWORD
    DB_NAME = "kong"
  }
}

data "template_cloudinit_config" "cloudinit-example" {
  gzip          = false
  base64_encode = false

  part {
    content_type = "text/x-shellscript"
    content      = data.template_file.shell-script.rendered
  }

}



# script/cloud-init.sh file
...
...
...

if true; then
    psql --host ${DB_HOST} --port=5432 --username=${DB_USERNAME} <<EOF      # creating database for kong api-gateway
CREATE DATABASE kong OWNER ${DB_USER}; 
EOF
fi

...
...
...

# Setup Configuration file

...
...
...
...
...
...
...

sudo -u kong kong migrations bootstrap

# Start Kong
sudo /usr/local/bin/kong start
# end of cloud-init.sh file

【问题讨论】:

  • 你能展示任何 TF 代码来演示你想要做什么以及你遇到的任何错误吗?
  • @Marcin 我用 tf 文件更新我的问题

标签: amazon-web-services amazon-ec2 terraform terraform-provider-aws terraform-template-file


【解决方案1】:

您应该可以使用 aws_instance 资源中的 depends_on 元参数来执行此操作。

尝试查看https://www.terraform.io/docs/language/meta-arguments/depends_on.html的官方文档

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 2021-06-17
    • 2017-02-14
    • 1970-01-01
    • 2018-02-08
    • 2021-03-02
    • 2019-03-21
    相关资源
    最近更新 更多