如何使用cloudformation模板获取用户数据的输出值?

Pra*_*790 5 instance amazon-web-services user-data aws-cloudformation

我有 cloudformation 模板,其中包含两个具有 userdata 属性的实例。

我需要从一个实例的用户数据中获取数据并将其传递给另一个实例的用户数据。

例如(如下),需要从实例1获取“test”,并传递给实例2用户数据。

示例模板:

"instance1": {
  "Type": "AWS::EC2::Instance",
  "Properties": {
"UserData": {
      "Fn::Base64": {
        "Fn::Join": [
          "",
          [
            "#!/bin/bash\n",
            "set -x\n",
            "echo test\n",
          ]]}}}},

"instance2": {
  "Type": "AWS::EC2::Instance",
  "Properties": {
"UserData": {
      "Fn::Base64": {
        "Fn::Join": [
          "",
          [
            "#!/bin/bash\n",
            "set -x\n",
            //fetch the value
          ]]}}}},
Run Code Online (Sandbox Code Playgroud)

Vor*_*Vor 4

基本上你有两个选择:

  1. 使用某种共享内存(ElastiCache、RDS、S3...)
  2. scp/ssh直接到另一个实例并获取您需要的信息。

我建议使用共享内存,例如 S3:

在实例 1 上:

echo "test" > /tmp/myfile
aws s3 cp /tmp/myfile s3://<bucket>/myfile
Run Code Online (Sandbox Code Playgroud)

在实例 2 上:

aws s3 cp s3://<bucket>/myfile /tmp/myfile
cat /tmp/myfile
Run Code Online (Sandbox Code Playgroud)