I have the following terraform script which creates Elastic Beanstalk with rds:
resource "aws_elastic_beanstalk_application" "elb_app" {
name = "my-app"
}
resource "aws_elastic_beanstalk_environment" "elb_env" {
name = "my-env"
application = aws_elastic_beanstalk_application.elb_app.name
solution_stack_name = data.aws_elastic_beanstalk_solution_stack.net_latest.name
setting {
namespace = "aws:elasticbeanstalk:application:environment"
name = "RDS_HOSTNAME"
value = # How can I set here the RDS endpoint ???
}
setting {
namespace = "aws:rds:dbinstance"
name = "HasCoupledDatabase"
value = "true"
}
setting {
namespace = "aws:rds:dbinstance"
name = "DBEngine"
value = var.db_engine
}
setting {
namespace = "aws:rds:dbinstance"
name = "DBEngineVersion"
value = var.db_engine_version
}
setting {
namespace = "aws:rds:dbinstance"
name = "DBInstanceClass"
value = var.db_instance_type
}
setting {
namespace = "aws:rds:dbinstance"
name = "DBPassword"
value = var.db_password
}
setting {
namespace = "aws:rds:dbinstance"
name = "DBUser"
value = var.db_user
}
}
The question is: how can I set the RDS_HOSTNAME
?
As far as I know, it is possible to get the endpoint from an RDS created as a separate resource, but I didn’t find any documentation how to get this from RDS created as part of an Elastic Beanstalk.
I spent many days and didn’t find any example however it should be a typical case for developers.
2
Answers
I have not tried it but according to the docs, these env variables are injected automatically by the elastic bean stalk:
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-nodejs.rds.html#nodejs-rds-connect
To validate it, you can create manually env environment with and RDS, then perform one of these validations:
env
.It looks like you have a pre-existing RDS. You can try creating an SSM parameter with a name like rds_host and store the host value in this param. Then you can refer it at the time of creating your cloudformation stack like this.
Reference: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/dynamic-references.html