2013-12-17 2 views
15

AWS 작성 및 스크립트를 사용하여 배포하는 중입니다. 스크립트를 작성하고 AWS 클라우드 형성 콘솔에서 스택 템플릿 스크립트를 실행했습니다.AWS - 클라우드 형성 S3 버킷 및 배포판을 생성하는 스크립트

스크립트와 정식 ID를 사용하여 S3 버킷 용 버킷 정책을 만듭니다. 버킷 정책을 만들면 스크립트에서 "OriginAccessIdentity"에 동적으로 할당하려고합니다. 버킷 정책에서 생성 된 ID를 "OriginAccessIdentity"속성에 추가하려고합니다.

이 기능을 달성하는 방법은 무엇입니까?

는 스크립트 :

{ 
    "AWSTemplateFormatVersion" : "2010-09-09", 

    "Description" : "AWS CloudFormation Template S3_With_CloudFront_Distribution", 

    "Parameters" : { 
     "bucketname" : { 
      "Type" : "String", 
      "Description" : "test"   
     }, 

     "cannonicalid" : { 
      "Type" : "String", 
      "Description" : "234213523145314534523452345234523452345"  
     } 
    }, 

    "Conditions" : { 
     "CreateProdResources" : {"Fn::Equals" : [{"Ref" : "EnvType"}, "dev"]} 
    }, 

    "Resources" : { 
     "testbucket" : { 
      "Type" : "AWS::S3::Bucket", 
      "Properties" : {  
      "BucketName" : { "Ref" : "bucketname" },   
      "WebsiteConfiguration" : { 
       "IndexDocument" : "index.html"    
      } 
      } 
     }, 


     "mybucketpolicy" : { 
      "Type" : "AWS::S3::BucketPolicy", 
      "Properties" : { 
       "PolicyDocument" : { 
       "Id" : "MyPolicy", 
       "Statement" : [ { 
        "Sid" : "Grant a CloudFront Origin Identity access to support private content", 
        "Action" : [ "s3:GetObject" ], 
        "Effect" : "Allow", 
        "Resource" : { "Fn::Join" : [ 
          "", [ "arn:aws:s3:::", { "Ref" : "testbucket" } , "/*" ] 
         ] }, 
        "Principal" : { 
         "CanonicalUser":{ "Ref" : "cannonicalid" } 
        } 
       } ] 
       }, 
       "Bucket" : { "Ref" : "testbucket" } 
       } 
     }, 


     "testdistribution" : { 
      "Type" : "AWS::CloudFront::Distribution", 
      "Properties" : { 
       "DistributionConfig" : { 
        "Origins" : [ { 
          "Id" : "S3Origin", 
          "DomainName" : { "Fn::GetAtt" : [ "testbucket", "DomainName" ] }, 
          "S3OriginConfig" : { 
           "OriginAccessIdentity" : "How to configure the id dynamically here" 
          } 
         } 
        ], 

        "Enabled" : "true", 
        "Comment" : "", 
        "DefaultRootObject" : "index.html",      
        "Aliases" : [ "test.com" ], 

        "CacheBehaviors" : [ { 
          "TargetOriginId" : "S3Origin", 
          "ForwardedValues" : { 
           "QueryString" : "false" 
          },        
          "ViewerProtocolPolicy" : "allow-all", 
          "MinTTL" : "1", 
          "PathPattern" : "resources/*.json" 
         } 
        ], 
        "DefaultCacheBehavior" : { 
         "TargetOriginId" : "S3Origin", 
         "ForwardedValues" : { 
          "QueryString" : "false" 
         },      
         "ViewerProtocolPolicy" : "allow-all", 
         "MinTTL" : "1" 
        } 
       } 
      } 
     } 
    }, 
    "Outputs" : { 
     "DistributionId" : { 
      "Description" : "CloudFront Distribution Id", 
      "Value" : { "Ref" : "testdistribution" } 
     }, 
     "DistributionName" : { 
      "Description" : "URL to access the CloudFront distribution", 
      "Value" : { "Fn::Join" : [ "", ["http://", {"Fn::GetAtt" : ["testdistribution", "DomainName"]} ]]} 
     }, 
     "S3OriginDNSName" : { 
      "Description" : "Name of S3 bucket to hold website content.", 
      "Value" : { "Fn::GetAtt" : [ "testbucket", "DomainName"] } 
     } 
    } 
} 
+0

:

"OriginAccessId": { "Type": "AWS::CloudFront::CloudFrontOriginAccessIdentity", "Properties": { "CloudFrontOriginAccessIdentityConfig": { "Comment": "MyDescription" } } } 

당신과 함께 배포 설정에 참조 할 수 있습니다 : 오리진 액세스 신원 자원과

는 다음과 같이 정의 클라우드에서 사용자를 생성하는 데 약간의 시간이 걸릴 수 있습니다. 따라서 .NET 코드를 사용하여 OAI 사용자를 생성 한 다음 스크립트에서 OAI 사용자를 사용합니다. OAI 사용자 생성을위한 다른 더 나은 솔루션이 있는지 알려주십시오. –

+0

은 .NET 코드를 사용하여 OAI를 작성하기위한 url을 참조합니다. http://irfanshirur.blogspot.in/2014/01/create-oai-and-canonical-user-using-net.html –

답변

6

원점 액세스 ID는 CloudFormation 만들 수 없습니다. CloudForm을 통해 사용 가능한 유일한 CloudFront 리소스는 AWS::CloudFront::Distribution 리소스입니다.

스택을 만들 때 매개 변수를 사용하여 기존 OAI를 전달함으로써 템플릿의 OAI에 대한 참조를 하드 코딩하지 않아도됩니다. 그런 다음 S3OriginConfig 키와 연결된 S3Origin 유형의 OriginAccessIdentity 값으로이 매개 변수를 사용할 수 있습니다.

이것은 이상적인 것은 아니지만 템플릿을보다 일반적인 것으로 만들 수 있습니다.

2

2011 년 11 월 2 일부터 CloudFormation은 AWS :: CloudFront :: CloudFrontOriginAccessIdentity 리소스를 사용하여 이것을 지원합니다. 구름 형성에 OAI 사용자를 생성하기 위해 바람직하지 스크립트를 사용

"OriginAccessIdentity" : { 
    "Fn::Sub": "origin-access-identity/cloudfront/${OriginAccessId}" 
}