Cloudformation Template

0

so I am looking to get MySQL install if I specify parameter environment as prod and if dev mariadb get installed from userdata script of this cloudformation template but it is not happening. Please guide on this how to do it? AWSTemplateFormatVersion: "2010-09-09" Parameters: Environment: Description: "The environment to deploy to (dev or prod)" Type: String Default: "dev" Resources: EC2Instance: Type: "AWS::EC2::Instance" Properties: InstanceType: "t2.micro" ImageId: "ami-0f8ca728008ff5af4" KeyName: "devops" SecurityGroupIds: - "sg-02464c840862fddaf" SubnetId: "subnet-0b2bbe1a860c1ec8f" UserData: !Base64 | #!/bin/bash if [ "${Environment}" == "prod" ]; then # Install MySQL on production instances sudo apt-get update sudo apt install mysql-server -y sudo systemctl restart mysql sudo systemctl enable mysql elif [ "${Environment}" == "dev" ]; then # Install MariaDB on development instances sudo apt-get update sudo apt install mariadb-server mariadb-client -y sudo systemctl enable mariadb fi Tags: - Key: "Name" Value: "MyNewInstance"

user01
질문됨 일 년 전224회 조회
1개 답변
0

You can do this with Conditions in the template, then test this condition and choose between defining the UserData property with the MySQL or MariaDB install. Something like this:

Conditions:
  CreateProdResources: !Equals [!Ref Environment, "prod"]

Then:

UserData: !If [CreateProdResources, !Base64 |
#!/bin/bash
# Install MySQL on production instances
sudo apt-get update
sudo apt install mysql-server -y
sudo systemctl restart mysql
sudo systemctl enable mysql, !Base64 |
# Install MariaDB on development instances
sudo apt-get update
sudo apt install mariadb-server mariadb-client -y
sudo systemctl enable mariadb]
profile pictureAWS
전문가
kentrad
답변함 일 년 전
profile picture
전문가
검토됨 일 년 전
  • AWSTemplateFormatVersion: "2010-09-09"

    Parameters: Environment: Description: "The environment to deploy to (dev or prod)" Type: String Default: ""

    Conditions: CreateProdResources: !Equals [!Ref Environment, "prod"]

    Resources: EC2Instance: Type: "AWS::EC2::Instance" Properties: InstanceType: "t2.micro" ImageId: "ami-0f8ca728008ff5af4" KeyName: "devops" SecurityGroupIds: - "sg-02464c840862fddaf" SubnetId: "subnet-0b2bbe1a860c1ec8f" UserData: !If - CreateProdResources - !Base64 | Fn::Sub: | #!/bin/bash
    sudo apt-get update sudo apt install mysql-server -y sudo systemctl restart mysql sudo systemctl enable mysql - !Base64 | Fn::Sub: | #!/bin/bash sudo apt-get update sudo apt install mariadb-server mariadb-client -y sudo systemctl enable mariadb Tags: - Key: "Name" Value: !If [CreateProdResources, "${Environment}-prod", "${Environment}-dev"]

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠