使用导出的值

时间:2019-05-13 05:17:54

标签: amazon-cloudformation

我可以使用此cloudformation模板导出密钥...

https://github.com/shantanuo/cloudformation/blob/master/restricted.template.txt

但是如何将保存的密钥直接导入另一个模板的“ UserData”部分?我试过了,但是不行...

aws-ec2-assign-elastic-ip --access-key !Ref {"Fn::ImportValue" : "accessKey" } --secret-key --valid-ips 35.174.198.170

其余模板(没有访问权限和密钥参考)正在按预期工作。

https://github.com/shantanuo/cloudformation/blob/master/security.template2.txt

2 个答案:

答案 0 :(得分:4)

因此,如果这是您执行导出的脚本(对不起,此脚本位于yaml中)

AWSTemplateFormatVersion: '2010-09-09'
Metadata:
  License: Apache-2.0
Description: 'AWS CloudFormation Sample Template'

Parameters:
  NewUsername:
    NoEcho: 'false'
    Type: String
    Description: New account username
    MinLength: '1'
    MaxLength: '41'
    ConstraintDescription: the username must be between 1 and 41 characters
  Password:
    NoEcho: 'true'
    Type: String
    Description: New account password
    MinLength: '1'
    MaxLength: '41'
    ConstraintDescription: the password must be between 1 and 41 characters

Resources:
  CFNUser:
    Type: AWS::IAM::User
    Properties:
      LoginProfile:
        Password: !Ref 'Password'
      UserName : !Ref 'NewUsername'
  CFNAdminGroup:
    Type: AWS::IAM::Group
  Admins:
    Type: AWS::IAM::UserToGroupAddition
    Properties:
      GroupName: !Ref 'CFNAdminGroup'
      Users: [!Ref 'CFNUser']
  CFNAdminPolicies:
    Type: AWS::IAM::Policy
    Properties:
      PolicyName: CFNAdmins
      PolicyDocument:
        Statement:
        - Effect: Allow
          Action: '*'
          Resource: '*'
          Condition:
            StringEquals:
              aws:RequestedRegion:
              - ap-south-1
              - us-east-1
      Groups: [!Ref 'CFNAdminGroup']
  CFNKeys:
    Type: AWS::IAM::AccessKey
    Properties:
      UserName: !Ref 'CFNUser'

Outputs:
  AccessKey:
    Value: !Ref 'CFNKeys'
    Description: AWSAccessKeyId of new user
    Export:
      Name: 'accessKey'
  SecretKey:
    Value: !GetAtt [CFNKeys, SecretAccessKey]
    Description: AWSSecretAccessKey of new user
    Export:
      Name: 'secretKey'

然后是一个示例,说明如何在导入cloudformation脚本中将这些值导入用户数据:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Test instance stack",
  "Parameters": {
    "KeyName": {
      "Description": "The EC2 Key Pair to allow SSH access to the instance",
      "Type": "AWS::EC2::KeyPair::KeyName"
    },
    "BaseImage": {
      "Description": "The AMI to use for machines.",
      "Type": "String"
    },
    "VPCID": {
      "Description": "ID of the VPC",
      "Type": "String"
    },
    "SubnetID": {
      "Description": "ID of the subnet",
      "Type": "String"
    }
  },
  "Resources": {
    "InstanceSecGrp": {
      "Type": "AWS::EC2::SecurityGroup",
      "Properties": {
        "GroupDescription": "Instance Security Group",
        "SecurityGroupIngress": [{
          "IpProtocol": "-1",
          "CidrIp": "0.0.0.0/0"
        }],
        "SecurityGroupEgress": [{
          "IpProtocol": "-1",
          "CidrIp": "0.0.0.0/0"
        }],
        "VpcId": {
          "Ref": "VPCID"
        }
      }
    },
    "SingleInstance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "KeyName": {
          "Ref": "KeyName"
        },
        "ImageId": {
          "Ref": "BaseImage"
        },
        "InstanceType": "t2.micro",
        "Monitoring": "false",
        "BlockDeviceMappings": [{
          "DeviceName": "/dev/xvda",
          "Ebs": {
            "VolumeSize": "20",
            "VolumeType": "gp2"
          }
        }],
        "NetworkInterfaces": [{
          "GroupSet": [{
            "Ref": "InstanceSecGrp"
          }],
          "AssociatePublicIpAddress": "true",
          "DeviceIndex": "0",
          "DeleteOnTermination": "true",
          "SubnetId": {
            "Ref": "SubnetID"
          }
        }],
        "UserData": {
          "Fn::Base64": {
            "Fn::Join": ["", [
              "#!/bin/bash -xe\n",
              "yum install httpd -y\n",
              "sudo sh -c \"echo ",
              { "Fn::ImportValue" : "secretKey" },
              " >> /home/ec2-user/mysecret.txt\" \n",
              "sudo sh -c \"echo ",
              { "Fn::ImportValue" : "accessKey" },
              " >> /home/ec2-user/myaccesskey.txt\" \n"
            ]]
          }
        }
      }
    }
  }
}

在此示例中,我只是将导入的值回显到文件中。如果您切换到SingleInstance并检查/var/lib/cloud/instance/scripts/part-001上的日志,那么您将看到服务器上用户数据脚本的外观。就我而言,该文件的内容是(键的值不是真实的):

#!/bin/bash -xe
yum install httpd -y
sudo sh -c "echo hAc7/TJA123143235ASFFgKWkKSjIC4 >> /home/ec2-user/mysecret.txt"
sudo sh -c "echo AKIAQ123456789123D >> /home/ec2-user/myaccesskey.txt"

以此为起点,您可以执行导入值所需的任何操作。

我已经使用上面的确切脚本对所有这些进行了测试,并且都可以正常工作。

答案 1 :(得分:0)

评论中建议的内容似乎是正确的。我可以使用ImportValue直接引用名称(例如本例中的'accessKey')!

AWSTemplateFormatVersion: '2010-09-09'
Metadata:
  License: Apache-2.0
Description: 'AWS CloudFormation Sample Template'

Resources:
  CFNUser:
    Type: AWS::IAM::User

Outputs:
  AccessKey:
    Value: 
      Fn::ImportValue: accessKey
    Description: AWSAccessKeyId of new user

例如如果其他模板已将其导出,则上述模板将返回accessKey的值。