通过CloudFormation在EC2上安装mysql客户端

时间:2017-01-12 01:07:01

标签: mysql amazon-ec2 amazon-cloudformation

要在我的EC2实例上安装MySQL客户端,请执行以下操作:

# Update all packages
sudo yum update

# Install mysql client 
sudo yum install mysql

我想知道在创建EC2实例时如何使用CloudFormation配置?

我可以参考哪些样品?

2 个答案:

答案 0 :(得分:2)

要在使用CloudFormation配置的EC2实例上运行简单脚本,请使用UserData资源的AWS::EC2::Instance属性。

示例:

Launch Stack

Description: Run a bash script using the UserData property.
Mappings:
  # amzn-ami-hvm-2016.09.1.20161221-x86_64-gp2
  RegionMap:
    us-east-1:
      "64": "ami-9be6f38c"
Resources:
  Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: !FindInMap [ RegionMap, !Ref "AWS::Region", 64]
      InstanceType: m3.medium
      UserData:
        "Fn::Base64":
          !Sub |
            #!/bin/bash
            # Update all packages
            yum -y update
            # Install mysql client 
            yum -y install mysql

答案 1 :(得分:0)

只想在上面的答案之上分享。

我后来发现我的EC2是通过EB部署的,而UserData似乎不起作用。

在这种情况下,我必须使用.ebextension' container_commands

所以在这里张贴它希望它也能帮到某人:

files:

  "/usr/local/bin/eb_mysql.sh":
    mode: "000755"
    owner: root
    group: root
    content: |        
        #!/bin/bash

        yum -y update
        yum -y install mysql          

container_commands:
  040_install_mysql_client:
    command: "chmod a+rx /usr/local/bin/eb_mysql.sh && /usr/local/bin/eb_mysql.sh"