git - 如何在 EC2 cfn-init 上克隆 CodeCommit 存储库?

标签 git amazon-web-services clone aws-cloudformation init

我尝试使用 CloudFormation 设置堆栈,并在 EC2 实例启动时我想从 CodeCommit 克隆存储库。该存储库来自其他 aws 帐户,因此我设置了具有正确权限的用户。
我尝试了很多不同的方法来克隆它,但都失败了。我尝试的最后一件事是直接在 UserData 中执行 git clone 但出现两个错误:

1- 带有 git clone https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/my-repo

fatal: could not read Username for 'https://git-codecommit.eu-west-1.amazonaws.com': No such device or address

2- 带有 git clone https://UserFormAWS-65456:/PASSWORDGENER<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d3c29383f243c2a2e3d1a1409501e1219181e1210101409531808500a180e09504c531c101c0712131c0a0e531e1210" rel="noreferrer noopener nofollow">[email protected]</a>/v1/repos/my-repo

fatal: unable to access 'https://TestUser-at-654654:/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3cbcacacac9c9c98ef3d4dac79ed0dcd7d6d0dcdededac79dd6c69ec4d6c0c79e829dd2ded2c9dcddd2c4c09dd0dcde" rel="noreferrer noopener nofollow">[email protected]</a>/v1/repos/my-repo/': Could not resolve host: TestUser-at-787897168481

这是我的模板:

{
"Resources": {
    "ec2Bastion": {
        "Type": "AWS::EC2::Instance",
        "Properties": {
            "InstanceType":"t2.micro",
            "AvailabilityZone" : "eu-west-1c",
            "BlockDeviceMappings" : [
               {
                  "DeviceName" : "/dev/xvda",
                  "Ebs" : {
                     "VolumeType" : "gp2",
                     "DeleteOnTermination" : "true",
                     "VolumeSize" : "8"
                  }
               }
            ],
            "DisableApiTermination": "false",
            "ImageId" : "ami-09693313102a30b2c",
            "KeyName" : "toto-aws",
            "Monitoring" : "true",
            "Tenancy" : "default",
            "NetworkInterfaces": [
                {
                    "AssociatePublicIpAddress": "true",
                    "DeviceIndex": "0",
                    "GroupSet": [{ "Ref": "sgBastion" }],
                    "SubnetId": "subnet-0c0ef68588036e3a3"
                }
            ]
        }
    },
    "ec2App": {
        "Type": "AWS::EC2::Instance",
        "Properties": {
            "InstanceType":"t3.large",
            "AvailabilityZone" : "eu-west-1c",
            "BlockDeviceMappings" : [
               {
                  "DeviceName" : "/dev/xvda",
                  "Ebs" : {
                     "VolumeType" : "gp2",
                     "DeleteOnTermination" : "true",
                     "VolumeSize" : "40"
                  }
               }
            ],
            "DisableApiTermination": "false",
            "ImageId" : "ami-025da7a468de72fee",
            "KeyName" : "toto-aws",
            "Monitoring" : "true",
            "Tenancy" : "default",
            "NetworkInterfaces": [
                {
                    "AssociatePublicIpAddress": "false",
                    "DeviceIndex": "0",
                    "GroupSet": [{ "Ref": "sgApp" }],
                    "SubnetId": { "Ref": "subnetApp" }
                }
            ],
            "UserData": {
                "Fn::Base64": {
                    "Fn::Join": [ "", [
                        "#!/bin/bash -xe\n",
                        "yum install -y aws-cfn-bootstrap\n",
                        "mkdir /root/.aws\n",
                        "# Install the files and packages from the metadata\n",
                        "/opt/aws/bin/cfn-init ",
                        "         --stack ", { "Ref" : "AWS::StackName" },
                        "         --resource ec2App ",
                        "         --configsets Configure ",
                        "         --region ", { "Ref" : "AWS::Region" }, "\n",
                        "# Signal the status from cfn-init\n",
                        "cd /var/www\n",
                        "git clone https://TestUser-at-7874456456781:/3fgh54wJ<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f221d3523193922162928797a7b3c0e7a1e720f28263b622c202b2a2c202222263b612a3a62382a3c3b627e612e222e3520212e383c612c2022" rel="noreferrer noopener nofollow">[email protected]</a>/v1/repos/my-repo; \n",
                        "/opt/aws/bin/cfn-signal -e $? ",
                        "         --stack ", { "Ref" : "AWS::StackName" },
                        "         --resource ec2App ",
                        "         --region ", { "Ref" : "AWS::Region" }, "\n"
                  ]]
                }
            }
        },
        "Metadata" : {
            "AWS::CloudFormation::Init" : {
                "configSets" : {
                    "Configure": ["Configure"]
                },
                "Configure": {
                    "files": {
                        "/root/.gitconfig": {
                            "content" : { "Fn::Join" : [ "", [
                                "[credential]\n",
                                "       helper = !aws codecommit credential-helper $@\n",
                                "       UseHttpPath = true\n"
                            ]]},
                            "mode"  : "000644",
                            "owner" : "root",
                            "group" : "root"
                        },
                        "/root/.aws/config": {
                            "content" : { "Fn::Join" : [ "", [
                                "[default]\n",
                                "region = eu-west-1\n",
                                "output = json\n"
                            ]]},
                            "mode"  : "000600",
                            "owner" : "root",
                            "group" : "root"
                        },
                        "/root/.aws/credentials": {
                            "content" : { "Fn::Join" : [ "", [
                                "[default]\n",
                                "aws_access_key_id = MYKEY\n",
                                "aws_secret_access_key = SECRETKEY\n"
                            ]]},
                            "mode"  : "000600",
                            "owner" : "root",
                            "group" : "root"
                        }
                    }
                }
            }
        }
    },
    "sgBastion": {
        "Type": "AWS::EC2::SecurityGroup",
        "Properties": {
            "VpcId": "vpc-0d2d3a7d301ffb3f2",
            "GroupDescription": "Enable SSH access via port 22",
            "SecurityGroupIngress": [
                {
                    "IpProtocol": "tcp",
                    "FromPort": "22",
                    "ToPort": "22",
                    "CidrIp" : "0.0.0.0/32"
                }
            ]
        }
    },
    "sgApp": {
        "Type": "AWS::EC2::SecurityGroup",
        "Properties": {
            "VpcId": "vpc-0d2d3a7d301ffb3f2",
            "GroupDescription": "Enable SSH access via port 22",
            "SecurityGroupIngress": [
                {
                    "IpProtocol": "tcp",
                    "FromPort": "22",
                    "ToPort": "22",
                    "SourceSecurityGroupId" : { "Ref": "sgBastion" }
                }
            ]
        }
    },
    "subnetApp" : {
        "Type" : "AWS::EC2::Subnet",
        "Properties" : {
            "VpcId": "vpc-0d2d3a7d301ffb3f2",
            "CidrBlock" : "10.0.10.0/24",
            "AvailabilityZone" : "eu-west-1c"
        }
    },
    "appSubnetRouteTableAssociation" : {
         "Type" : "AWS::EC2::SubnetRouteTableAssociation",
         "Properties" : {
            "SubnetId" : { "Ref" : "subnetApp" },
            "RouteTableId" : { "Ref" : "appRouteTable" }
         }
     },
     "appRouteTable" : {
         "Type" : "AWS::EC2::RouteTable",
         "Properties" : {
            "VpcId" : "vpc-0d2d3a7d301ffb3f2"
         }
     },
     "appRoute" : {
        "Type" : "AWS::EC2::Route",
        "Properties" : {
            "RouteTableId" : { "Ref" : "appRouteTable" },
            "DestinationCidrBlock" : "0.0.0.0/0",
            "NatGatewayId" : "nat-027c1f24384fc90e6"
        }
     }
  }

}

你有什么想法吗?

提前致谢。

最佳答案

关于用户名:密码的使用,语法中不应在密码中包含任何“/”:

https://UserFormAWS-65456:/PASSWORDGENERATEBYAWS@
                          ?
# should be:
https://UserFormAWS-65456:PASSWORDGENERATEBYAWS@

(除非“/”实际上是您密码的一部分)

此外,该密码中的任何特殊字符都必须是 percent encoded
例如,将“=”替换为 %3D
如果 / 实际上是您密码的第一个字符,则应将其替换为 %2F

关于git - 如何在 EC2 cfn-init 上克隆 CodeCommit 存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53825996/

相关文章:

git - 在每次提交中设置用户名

amazon-web-services - EC2 实例可以设置为来自不同的 IP 范围吗?

amazon-web-services - 获取DynamoDB表的最新更新时间

reference - 有没有办法将引用类型克隆到拥有的类型中?

java - 了解数组的克隆方法

git - 如何验证git中每个提交的作者

Git 忽略子目录中的文件

git - 如何克隆本地 svn 存储库?

git - 重新初始化 Git 仓库

amazon-web-services - AWS CloudFormation 条件模板验证