云盘算-高级云资源配置(Advanced Cloud Provisioning)

打印 上一主题 下一主题

主题 525|帖子 525|积分 1579

向Bucket添加公共访问(Adding Public Access to Bucket)

        在模块5中,我们已经看到怎样利用CloudFormation创建和更新一个Bucket。现在我们将进一步更新该Bucket,添加公共访问权限。我们在模块5中利用的模板(third_template.txt)如下所示:
  1. {
  2.     "Resources": {
  3.         "cloudformationbucket" : {
  4.             "Type": "AWS::S3::Bucket",
  5.             "DeletionPolicy": "Delete",
  6.             "Properties": {
  7.                 "BucketName": "pchakrab-cf-bucket-new"
  8.             }   
  9.         }
  10.     }
  11. }
复制代码
当前的Bucket未表现为“公共”,如下所示:

(插图表现当前的Bucket未被设置为公共)

        让我们更新模板,以便利用此CloudFormation堆栈自动托管静态网站。起首,我们将index.html和error.html(与模块2中的文件相同)上传到我们要托管网站的Bucket(在本例中为pchakrab-cf-bucket-new)。现在我们将在上面的模板中的“Properties”标签中添加“AccessControl”和“WebsiteConfiguration”属性。我们将为访问控制指定“PublicRead”,并为网站配置指定index和error文件。更新后的模板如下所示:
  1. {
  2.     "Resources": {
  3.         "cloudformationbucket" : {
  4.             "Type": "AWS::S3::Bucket",
  5.             "DeletionPolicy": "Delete",
  6.             "Properties": {
  7.                 "BucketName": "pchakrab-cf-bucket-new",
  8.                 "AccessControl": "PublicRead",
  9.                 "WebsiteConfiguration": {
  10.                     "IndexDocument": "index.html",
  11.                     "ErrorDocument": "error.html"
  12.                 }
  13.             }   
  14.         }
  15.     }
  16. }
复制代码
        我们现在将这个模板更新为“fourth_template.txt”并上传到Bucket中,然后复制此模板的对象URL。接下来,按照我们在模块5中看到的相同步骤更新我们的堆栈。更新堆栈后,Bucket将答应公共读取,静态网站将自动启用,如下所示:


(插图表现更新后的Bucket配置)

        我们仍然无法打开网站,由于缺少战略声明。因此,我们需要再次更新我们的堆栈。我们将在下一个主题中看到这一点。
利用CloudFormation添加Bucket战略(Adding Bucket Policy using CloudFormation)

        延续上一个主题,我们现在利用CloudFormation堆栈添加一个Bucket战略,以便可以通过欣赏器访问网站。请注意,我们需要利用我们利用的逻辑名称(例如,“cloudformationbucket”)作为“Ref”。这是我们在模块2中利用的相同Bucket战略。我们进行了两处更改 - 在“Ref”(我们在这里给出了逻辑名称)和“Resource”(我们给出了物理名称/arn)。我们必须更新当前的模板并上传到S3 Bucket。我们上次利用的模板如下:
        起首,假如Bucket访问未设置为“公共”,我们需要取消选中“权限”选项卡中的“阻止全部公共访问”。
  1. {
  2.     "Resources": {
  3.         "cloudformationbucket" : {
  4.             "Type": "AWS::S3::Bucket",
  5.             "DeletionPolicy": "Delete",
  6.             "Properties": {
  7.                 "BucketName": "pchakrab-cf-bucket-new",
  8.                 "AccessControl": "PublicRead",
  9.                 "WebsiteConfiguration": {
  10.                     "IndexDocument": "index.html",
  11.                     "ErrorDocument": "error.html"
  12.                 }
  13.             }   
  14.         }
  15.         //我们将在此处添加我们的策略......
  16.     }
  17. }
复制代码
        “BucketPolicy”是一个类似于“Bucket”的资源,它具有“Type”和“Properties”。“Properties”标签包罗一个引用的“Bucket”和一个“PolicyDocument”标签。我们通过其逻辑名称在“Bucket”标签中添加对工作Bucket的引用。“PolicyDocument”进一步包罗一个属性,称为“Statement”。“Statement”属性包罗五个子属性 - “Sid”,“Effect”,“Principal”,“Action”和“Resource”。“Resource”属性需要Bucket的ARN(包罗物理名称)。 “Principal”是“Statement”中的强制性子属性。战略如下所示:
  1. "samplebucketpolicy" : {
  2.     "Type" : "AWS::S3::BucketPolicy",
  3.     "Properties" : {
  4.         "Bucket" : {"Ref" : "cloudformationbucket"},
  5.         "PolicyDocument": {
  6.             "Statement": [{
  7.                 "Sid": "AllowPublicRead",
  8.                 "Effect": "Allow",
  9.                 "Principal": {
  10.                     "AWS": "*"
  11.                 },
  12.                 "Action": "s3:GetObject",
  13.                 "Resource": "arn:aws:s3:::pchakrab-cf-bucket-new/*"
  14.             }]
  15.         }
  16.     }
  17. }
复制代码
        添加战略后的更新模板如下所示:
  1. {
  2.     "Resources": {
  3.         "cloudformationbucket" : {
  4.             "Type": "AWS::S3::Bucket",
  5.             "DeletionPolicy": "Delete",
  6.             "Properties": {
  7.                 "BucketName": "pchakrab-cf-bucket-new",
  8.                 "AccessControl": "PublicRead",
  9.                 "WebsiteConfiguration": {
  10.                     "IndexDocument": "index.html",
  11.                     "ErrorDocument": "error.html"
  12.                 }
  13.             }   
  14.         },
  15.         "samplebucketpolicy" : {
  16.             "Type" : "AWS::S3::BucketPolicy",
  17.             "Properties" : {
  18.                 "Bucket" : {"Ref" : "cloudformationbucket"},
  19.                 "PolicyDocument": {
  20.                     "Version": "2008-10-17",
  21.                     "Statement": [{
  22.                         "Sid": "AllowPublicRead",
  23.                         "Effect": "Allow",
  24.                         "Principal": {
  25.                             "AWS": "*"
  26.                         },
  27.                         "Action": "s3:GetObject",
  28.                         "Resource": "arn:aws:s3:::pchakrab-cf-bucket-new/*"
  29.                     }]
  30.                 }
  31.             }
  32.         }
  33.     }
  34. }
复制代码
        我们可以将此模板上传到S3 Bucket并复制对象URL。然后,我们可以按照之前的步骤更新堆栈。一旦堆栈更新完毕,我们将在Bucket的“权限”选项卡中看到添加的Bucket战略。
利用CloudFormation创建DynamoDB表(Creating a DynamoDB Table with CloudFormation)

        我们可以利用CloudFormation模板创建DynamoDB表。创建DynamoDB表的模板具有以下语法:
  1. {
  2.   "Type" : "AWS::DynamoDB::Table",
  3.   "Properties" : {
  4.       "AttributeDefinitions" : [ AttributeDefinition, ... ],
  5.       "BillingMode" : String,
  6.       "ContributorInsightsSpecification" : ContributorInsightsSpecification,
  7.       "GlobalSecondaryIndexes" : [ GlobalSecondaryIndex, ... ],
  8.       "KeySchema" : [ KeySchema, ... ],
  9.       "KinesisStreamSpecification" : KinesisStreamSpecification,
  10.       "LocalSecondaryIndexes" : [ LocalSecondaryIndex, ... ],
  11.       "PointInTimeRecoverySpecification" : PointInTimeRecoverySpecification,
  12.       "ProvisionedThroughput" : ProvisionedThroughput,
  13.       "SSESpecification" : SSESpecification,
  14.       "StreamSpecification" : StreamSpecification,
  15.       "TableClass" : String,
  16.       "TableName" : String,
  17.       "Tags" : [ Tag, ... ],
  18.       "TimeToLiveSpecification" : TimeToLiveSpecification
  19.     }
  20. }
复制代码
        上述语法应嵌入在“Resources”标签中,如我们之前在S3 Bucket中看到的。我们通常不需要利用全部这些属性。我们可以利用以下模板创建一个带有主键“Id”的简单“Employee”表:
  1. {
  2.     "Resources": {
  3.         "EmployeeTable": {
  4.             "Type": "AWS::DynamoDB::Table",
  5.             "Properties": {
  6.                 "TableName": "Employee",
  7.                 "AttributeDefinitions": [
  8.                     {
  9.                         "AttributeName" : "Id",
  10.                         "AttributeType" : "N"   
  11.                     }
  12.                 ],
  13.                 "KeySchema": [
  14.                     {
  15.                         "AttributeName" : "Id",
  16.                         "KeyType" : "HASH"
  17.                     }
  18.                 ],
  19.                 "ProvisionedThroughput": {
  20.                     "ReadCapacityUnits": 5,
  21.                     "WriteCapacityUnits": 5
  22.                 }
  23.             }
  24.         }
  25.     }
  26. }
复制代码
        “EmployeeTable”是资源的逻辑名称。接下来,我们需要指定资源的“Type”,它将是一个DynamoDB表,即“AWS:ynamoDB::Table”。表的属性包罗“TableName”、“AttributeDefinitions”、“KeySchema”和“ProvisionedThroughput”。我们不需要添加全部表属性,只需添加与主键相关的属性。 “KeySchema”界说了在“AttributeDefinitions”中界说的主键类型。指定表的吞吐量,包罗读取和写入的值。
DynamoDB表

        利用上述部分中表现的CloudFormation模板创建一个DynamoDB表。您需要利用差别的表名称和逻辑名称。
模板参数(Template Parameter)

        一个示例参数语法如下所示:
  1. "Parameters" : {
  2.   "ParameterLogicalID" : {
  3.     "Type": "DataType",
  4.     "ParameterProperty" : "value"
  5.   }
  6. }
复制代码
        这是我们怎样将此参数的引用添加到我们的资源中。
  1. "SomeResourceLogicalID" : {
  2.   "Type": "type",
  3.   "Properties" : {
  4.     "SomeProperty" : { "Ref" : "ParameterLogicalID" },
  5.     ....................................
  6.   }
  7. }
复制代码
        我们现在将看到怎样利用参数添加主键的示例,为此我们将稍微更新我们上次利用的模板。让我们创建一个名为“EmployeeNew”的“Employee”表的副本。
  1. {
  2.     "Parameters": {
  3.         "PrimaryKeyName": {
  4.             "Type": "String",
  5.             "Default": "EmployeeId",
  6.             "Description": "Primary Key Name"
  7.         },
  8.         "PrimaryKeyType": {
  9.             "Type": "String",
  10.             "Default": "N",
  11.             "Description": "Primary Key Type"
  12.         }
  13.     },
  14.     "Resources": {
  15.         "EmployeeTableNew": {
  16.             "Type": "AWS::DynamoDB::Table",
  17.             "Properties": {
  18.                 "TableName": "EmployeeNew",
  19.                 "AttributeDefinitions": [
  20.                     {
  21.                         "AttributeName" : {"Ref": "PrimaryKeyName"},
  22.                         "AttributeType" : {"Ref": "PrimaryKeyType"}   
  23.                     }
  24.                 ],
  25.                 "KeySchema": [
  26.                     {
  27.                         "AttributeName" : {"Ref": "PrimaryKeyName"},
  28.                         "KeyType" : "HASH"
  29.                     }
  30.                 ],
  31.                 "ProvisionedThroughput": {
  32.                     "ReadCapacityUnits": 5,
  33.                     "WriteCapacityUnits": 5
  34.                 }
  35.             }
  36.         }
  37.     }
  38. }
复制代码
        每个参数都有一个类型,并界说了资源的必要属性,在我们的例子中是主键的名称和类型。我们可以在CloudFormation堆栈中应用模板以查察创建的DynamoDB表。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

篮之新喜

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表