{"id":10084,"date":"2023-03-22T13:07:19","date_gmt":"2023-03-22T05:07:19","guid":{"rendered":"http:\/\/123.57.164.21\/?p=10084"},"modified":"2023-03-22T13:07:19","modified_gmt":"2023-03-22T05:07:19","slug":"%e3%80%90aws%e3%80%91iam-%e7%b3%bb%e5%88%97-%e5%9f%ba%e4%ba%8e%e8%a7%92%e8%89%b2%ef%bc%88role-base%ef%bc%89%e7%9a%84%e6%9d%83%e9%99%90%e6%8e%a7%e5%88%b6","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=10084","title":{"rendered":"\u3010AWS\u3011IAM \u7cfb\u5217 \u57fa\u4e8e\u89d2\u8272\uff08role base\uff09\u7684\u6743\u9650\u63a7\u5236"},"content":{"rendered":"\n<p><strong>\u524d\u8a00<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/docs.aws.amazon.com\/IAM\/latest\/UserGuide\/introduction.html\" target=\"_blank\" rel=\"noreferrer noopener\">IAM<\/a> \u5168\u79f0 Identity and Access Management\uff0c\u4e2d\u6587\u7ffb\u8bd1\u4e3a\u8eab\u4efd\u548c\u63a7\u5236\u7ba1\u7406\uff0c\u662f AWS \u63d0\u4f9b\u7684\u4e00\u9879\u514d\u8d39\u7684\u6743\u9650\u63a7\u5236\u670d\u52a1\u3002IAM \u662f\u4e91\u670d\u52a1\u5b89\u5168\u6027\u7684\u57fa\u77f3\uff0c\u662f\u5f00\u53d1\u8005\u5fc5\u987b\u638c\u63e1\u7684\u6280\u80fd\u4e4b\u4e00\u3002\u8bd5\u60f3\u5982\u679c\u6240\u6709\u8d44\u6e90\u90fd\u6ca1\u6709\u6743\u9650\u63a7\u5236\uff0c\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u968f\u610f\u4fee\u6539\u548c\u5220\u9664\u8d44\u6e90\uff0c\u8fd9\u5bf9\u4e8e\u4e91\u7aef\u670d\u52a1\u6765\u8bf4\u662f\u4e00\u4ef6\u591a\u4e48\u5371\u9669\u7684\u4e8b\u60c5\u3002<\/p>\n\n\n\n<p>IAM \u63d0\u4f9b\u4e86\u591a\u79cd\u6743\u9650\u63a7\u5236\u65b9\u6848\uff0c\u6709\u57fa\u4e8e\u7528\u6237\u7684\uff0c\u4e5f\u6709\u57fa\u4e8e\u89d2\u8272\u7684\u3002\u8fd9\u4e9b\u65b9\u6848\u90fd\u80fd\u5b9e\u73b0\u6743\u9650\u63a7\u5236\u7684\u529f\u80fd\uff0c\u56e0\u6b64\u521d\u6b21\u63a5\u89e6 IAM \u7684\u5f00\u53d1\u8005\u901a\u5e38\u90fd\u4f1a\u5728\u65b9\u6848\u9009\u62e9\u4e0a\u9677\u5165\u8ff7\u832b\u3002<\/p>\n\n\n\n<p>\u57fa\u4e8e\u89d2\u8272\u7684\u6743\u9650\u63a7\u5236\u662f\u4f7f\u7528\u6700\u5e7f\u6cdb\uff0c\u4e5f\u662f\u6700\u63a8\u8350\u7684\u6743\u9650\u63a7\u5236\u65b9\u6848\u3002\u672c\u6587\u7684\u76ee\u6807\u662f\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u5feb\u901f\u7406\u89e3\u5e76\u4e0a\u624b\u57fa\u4e8e\u89d2\u8272\u7684\u6743\u9650\u63a7\u5236\u3002<\/p>\n\n\n\n<p><strong>\u6982\u5ff5<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>IAM \u7684\u6743\u9650\u63a7\u5236\u5305\u542b\u4e09\u4e2a\u7ef4\u5ea6\uff08\u4ee5\u4e0b\u7b80\u79f0\u4e3a\u4e09\u8981\u7d20\uff09\uff0c\u5373\uff1a<\/p>\n\n\n\n<ol><li><strong>\u8c01<\/strong>\u80fd\uff08\u6216\u4e0d\u80fd\uff09\u5bf9<\/li><li>\u4ec0\u4e48<strong>\u8d44\u6e90<\/strong><\/li><li><strong>\u505a\u4ec0\u4e48<\/strong><\/li><\/ol>\n\n\n\n<p>\u4f8b\u5982\uff1a\u67d0 EC2 \u5b9e\u4f8b\u53ef\u4ee5\u5411\u67d0 S3 bucket \u4e0a\u4f20\u6587\u4ef6\u3002\u8fd9\u91cc\u7684\u4e09\u8981\u7d20\u5206\u522b\u662f\uff1a<\/p>\n\n\n\n<p>1.\u8c01\uff1a\u67d0 EC2 <\/p>\n\n\n\n<p>2.\u5b9e\u4f8b\u8d44\u6e90\uff1a\u67d0 S3 bucket<\/p>\n\n\n\n<p>3.\u505a\u4ec0\u4e48\uff1a\u4e0a\u4f20\u6587\u4ef6<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/image-213.png\" alt=\"\" class=\"wp-image-10085\" width=\"355\" height=\"181\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-213.png 636w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-213-300x153.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-213-230x117.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-213-350x178.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-213-480x245.png 480w\" sizes=\"(max-width: 355px) 100vw, 355px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u8c01\uff08role\uff09<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5728\u57fa\u4e8e\u89d2\u8272\uff08role base\uff09\u7684\u6743\u9650\u63a7\u5236\u4e2d\uff0c\u89d2\u8272\u5c31\u662f\u4e09\u8981\u7d20\u4e2d\u7684\u201c<strong>\u8c01<\/strong>\u201d\u3002\u4e00\u4e2a\u89d2\u8272\u53ef\u4ee5\u88ab\u5173\u8054\u5230\u591a\u4e2a\u59d4\u6258\u4eba\uff0c\u6240\u6709\u88ab\u5173\u8054\u7684\u59d4\u6258\u4eba\u90fd\u4f1a\u6210\u4e3a IAM \u4e09\u8981\u7d20\u4e2d\u201c<strong>\u8c01<\/strong>\u201d\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/image-214.png\" alt=\"\" class=\"wp-image-10086\" width=\"400\" height=\"326\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-214.png 742w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-214-300x244.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-214-230x187.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-214-350x285.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-214-480x391.png 480w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/figure><\/div>\n\n\n\n<p>\u89d2\u8272\u201c<strong>S3 \u4e0a\u4f20\u8005<\/strong>\u201d\u88ab\u5173\u8054\u5230\u4e86\u4e09\u4e2a\u59d4\u6258\u4eba\uff08EC2\uff0cECS\uff0cLambda\uff09\u3002\u5173\u8054\u540e\uff0c\u8fd9\u4e09\u4e2a\u59d4\u6258\u4eba\u5c31\u90fd\u53ef\u4ee5\u88ab\u770b\u4f5c\u662f\u201c<strong>S3 \u4e0a\u4f20\u8005<\/strong>\u201d\u8fd9\u4e2a\u89d2\u8272\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u8bfb\u8005\u5bf9\u89d2\u8272\u548c\u59d4\u6258\u4eba\u7684\u5173\u7cfb\u8fd8\u4e0d\u662f\u5f88\u6e05\u695a\uff0c\u53ef\u4ee5\u7528 Java \u4e2d\u7684\u7c7b\u548c\u63a5\u53e3\u6765\u505a\u4e00\u4e0b\u7c7b\u6bd4\u3002\u89d2\u8272\u5c31\u50cf\u4e00\u4e2a\u63a5\u53e3\uff08interface\uff09\uff0c\u59d4\u6258\u4eba\u5c31\u662f\u5b9e\u73b0\u4e86\u8fd9\u4e2a\u63a5\u53e3\u7684\u7c7b\uff08class\uff09\uff0c\u8fd9\u4e9b\u7c7b\u90fd\u53ef\u4ee5\u88ab\u89c6\u4e3a\u8be5\u63a5\u53e3\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">interface SomeRole { }\n\nclass EC2 implements SomeRole { }\nclass ECS implements SomeRole { }\nclass Lambda implements SomeRole { }<\/pre>\n\n\n\n<p><strong>\u8d44\u6e90\uff08resource\uff09<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u8d4b\u4e88\u4e0a\u8ff0\u89d2\u8272\u4e00\u4e9b\u8d44\u6e90\u7684\u6743\u9650\uff0c\u8fd9\u4e9b\u8d44\u6e90\u53ef\u4ee5\u662f\u4efb\u610f\u4e00\u4e2a AWS \u8d44\u6e90\u3002\u4f8b\u5982\u67d0\u4e2a S3 bucket\uff0c\u67d0\u4e2a\u6570\u636e\u5e93\uff0c\u67d0\u4e2a\u6d88\u606f\u961f\u5217\u7b49\u3002<\/p>\n\n\n\n<p><strong>\u505a\u4ec0\u4e48\uff08action\uff09<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>AWS \u4e3a\u6bcf\u4e2a\u8d44\u6e90\u90fd\u505a\u4e86\u975e\u5e38\u7cbe\u7ec6\u7684\u6743\u9650\u63a7\u5236\u3002\u4f8b\u5982 S3 \u7684\u6743\u9650\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul><li>s3:CreateBucket<\/li><li>s3:ListAllMyBuckets<\/li><li>s3:GetBucketLocation<\/li><li>s3:PutObject<\/li><\/ul>\n\n\n\n<p>\u8fd9\u8fd8\u53ea\u662f\u51b0\u5c71\u4e00\u89d2\u3002\u5230\u76ee\u524d\u4e3a\u6b62\uff0cS3 \u603b\u5171\u6709\u4e0a\u767e\u4e2a\u63a7\u5236\u6743\u9650\uff0c\u8be6\u60c5\u53ef\u4ee5\u770b<a rel=\"noreferrer noopener\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/docs.aws.amazon.com\/AmazonS3\/latest\/userguide\/list_amazons3.html\" target=\"_blank\">\u8fd9\u91cc<\/a>\u3002<\/p>\n\n\n\n<p><strong>\u7b56\u7565\uff08policy\uff09<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>IAM \u628a\u4e0a\u8ff0\u4e09\u8981\u7d20\u7ec4\u7ec7\u5728\u4e00\u4e2a json \u6587\u4ef6\u4e2d\uff0c\u5e76\u4e14\u7ed9\u8fd9\u4e2a json \u8d77\u4e86\u4e2a\u540d\u5b57\uff0c\u53eb\u505a<strong>\u7b56\u7565<\/strong>\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n    \"Version\": \"2012-10-17\",\n    \"Statement\": [\n        {\n            \"Effect\": \"Allow\",\n            \"Action\": [\n                \"s3:PutObject\"\n            ],\n            \"Resource\": \"arn:aws:s3:::awsexamplebucket1\/*\"\n        }\n    ]\n}\n<\/pre>\n\n\n\n<ul><li>Version \u662f\u7b56\u7565\u8bed\u8a00\u7248\u672c\uff0c\u6700\u65b0\u7248\u4e3a 2012-10-17\u3002<\/li><li>Statement \u662f\u4e00\u7cfb\u5217\u4e09\u5143\u7d20\u7684\u96c6\u5408\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e00\u4e2a\u7b56\u7565\u53ef\u4ee5\u914d\u7f6e\u591a\u4e2a\u4e09\u5143\u7d20\u63a7\u5236\u6743\u9650\u3002<\/li><li>Effect \u7684\u53d6\u503c\u6709 Allow \u548c Deny\uff0c\u5206\u522b\u8868\u793a\u5141\u8bb8\u6216\u62d2\u7edd\u64cd\u4f5c\u3002\u793a\u4f8b\u4e2d\u4f7f\u7528\u7684\u662f Allow\uff0c\u8868\u793a\u5141\u8bb8 Action \u4e2d\u7684\u64cd\u4f5c\u3002<\/li><li>Action \u5c31\u662f\u5bf9\u8d44\u6e90\uff08resource\uff09\u7684\u64cd\u4f5c\uff0c\u662f\u4e2a\u6570\u7ec4\uff0c\u56e0\u6b64\u53ef\u4ee5\u5b9a\u4e49\u5bf9\u67d0\u4e00\u8d44\u6e90\u7684\u591a\u4e2a\u64cd\u4f5c\u3002<\/li><li>Resource \u5c31\u662f\u88ab\u63a7\u5236\u7684\u8d44\u6e90\uff0c\u793a\u4f8b\u4e2d\u7684\u662f\u4e00\u4e2a S3 bucket\u3002<\/li><\/ul>\n\n\n\n<p><strong>\u7b56\u7565\u4e0e\u89d2\u8272\u7684\u5173\u7cfb<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u7ec6\u5fc3\u7684\u8bfb\u8005\u4f1a\u53d1\u73b0\uff0c\u8d44\u6e90\uff08resource\uff09\u548c\u64cd\u4f5c\uff08action\uff09\u5df2\u7ecf\u4f53\u73b0\u5728\u7b56\u7565\u4e2d\u4e86\uff0c\u4f46\u662f\u4f3c\u4e4e\u6ca1\u6709\u770b\u5230\u89d2\u8272\uff08role\uff09\u7684\u5b9a\u4e49\u3002\u5176\u5b9e\u89d2\u8272\u548c\u7b56\u7565\u662f\u4e24\u4e2a\u72ec\u7acb\u7684\u5b9e\u4f53\uff0c\u901a\u8fc7\u5c06\u7b56\u7565\u5173\u8054\u5230\u89d2\u8272\u6765\u5efa\u7acb\u4e8c\u8005\u7684\u8054\u7cfb\u3002\u4e00\u4e2a\u89d2\u8272\u53ef\u4ee5\u88ab\u5173\u8054<strong>\u591a\u4e2a<\/strong>\u7b56\u7565\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u628a\u4e09\u5143\u7d20\u548c\u7b56\u7565\u653e\u5728\u4e00\u8d77\uff0c\u4ed6\u4eec\u4f1a\u662f\u5982\u4e0b\u7684\u5173\u7cfb\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/image-215.png\" alt=\"\" class=\"wp-image-10087\" width=\"438\" height=\"164\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-215-230x87.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-215-350x132.png 350w\" sizes=\"(max-width: 438px) 100vw, 438px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u5b9e\u6218\u6f14\u7ec3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u638c\u63e1\u4e86 IAM \u4e09\u8981\u7d20\u548c\u7b56\u7565\u4e4b\u540e\uff0c\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5b9e\u6218\u6f14\u7ec3\u6765\u5c55\u793a\u4e00\u4e0b\u57fa\u4e8e\u89d2\u8272\u7684\u6743\u9650\u63a7\u5236\u5177\u4f53\u662f\u5982\u4f55\u4f7f\u7528\u7684\u3002<\/p>\n\n\n\n<p>\u5728\u5b9e\u6218\u6f14\u7ec3\u4e2d\uff0c\u6211\u4eec\u4f1a\u521b\u5efa\u4e00\u4e2a <a rel=\"noreferrer noopener\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/docs.aws.amazon.com\/lambda\/latest\/dg\/welcome.html\" target=\"_blank\">Lambda<\/a>\uff0c\u5e76\u901a\u8fc7\u89d2\u8272\u8d4b\u4e88\u8be5 Lambda \u8bfb\u53d6\u6240\u6709 S3 bucket \u7684\u6743\u9650\uff0c\u5982\u56fe\u6240\u793a\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/03\/image-216-1024x338.png\" alt=\"\" class=\"wp-image-10088\" width=\"513\" height=\"169\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-216-1024x338.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-216-300x99.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-216-768x253.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-216-830x274.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-216-230x76.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-216-350x115.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-216-480x158.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/image-216.png 1274w\" sizes=\"(max-width: 513px) 100vw, 513px\" \/><\/figure><\/div>\n\n\n\n<p><strong>\u521b\u5efa Lambda<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u6211\u4eec\u4f9d\u7136\u57fa\u4e8e <a rel=\"noreferrer noopener\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/docs.aws.amazon.com\/cdk\/v2\/guide\/home.html\" target=\"_blank\">CDK<\/a> \u6765\u521b\u5efa\u6240\u6709\u8d44\u6e90\u3002\u4f7f\u7528 CDK \u7684\u597d\u5904\u662f\u4f60\u65e0\u9700\u624b\u52a8\u53bb AWS Console \u505a\u8d44\u6e90\u7684\u521b\u5efa\u548c\u914d\u7f6e\uff0c\u4ec5\u9700\u901a\u8fc7\u4ee3\u7801\u5c31\u53ef\u5b9e\u73b0\u5168\u90e8\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p><strong>\u521d\u59cb\u5316 CDK \u9879\u76ee<\/strong><\/p>\n\n\n\n<p>\u521b\u5efa\u5e76\u8fdb\u5165\u4e00\u4e2a\u9879\u76ee\u6587\u4ef6\u5939\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mkdir iam-role-lambda\ncd iam-role-lambda<\/pre>\n\n\n\n<p>\u521d\u59cb\u5316 CDK \u9879\u76ee\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cdk init app --language typescript<\/pre>\n\n\n\n<p><strong>\u521b\u5efa\u57fa\u4e8e Python \u7684 Lambda<\/strong><\/p>\n\n\n\n<p>\u7528\u5982\u4e0b\u4ee3\u7801\u66ff\u6362 <strong>.\/lib\/iam-role-lambda-stack.ts<\/strong> \u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import { Stack, StackProps } from 'aws-cdk-lib';\nimport { Construct } from 'constructs';\nimport * as lambda from 'aws-cdk-lib\/aws-lambda';\n\nexport class IamRoleLambdaStack extends Stack {\n  constructor(scope: Construct, id: string, props?: StackProps) {\n    super(scope, id, props);\n\n    new lambda.Function(this, 'MyFunction', {\n      runtime: lambda.Runtime.PYTHON_3_9,\n      handler: 'app.lambda_handler',\n      code: lambda.Code.fromAsset('.\/my_function'),\n    });\n  }\n}\n<\/pre>\n\n\n\n<p><strong>\u6dfb\u52a0 Python \u4ee3\u7801<\/strong><\/p>\n\n\n\n<p>\u521b\u5efa Python \u4ee3\u7801\u6587\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mkdir my_function\ncd my_function\ntouch app.py<\/pre>\n\n\n\n<p>\u5c06\u5982\u4e0b\u4ee3\u7801\u6dfb\u52a0\u5230 <strong>.\/my_function\/app.py<\/strong> \u6587\u4ef6\u4e2d\u3002\u8fd9\u662f\u4e00\u6bb5 Lambda \u6267\u884c\u7684 Python \u4ee3\u7801\uff0c\u4f5c\u7528\u662f\u7f57\u5217\u51fa\u6211\u7684 AWS \u5e10\u53f7\u4e2d\u7684\u6240\u6709 S3 bucket\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import boto3\n\ndef lambda_handler(event, context):\n    # Retrieve the list of existing buckets\n    s3 = boto3.client('s3')\n    response = s3.list_buckets()\n\n    # Output the bucket names\n    print('Existing buckets:')\n    for bucket in response['Buckets']:\n        print(f'  {bucket[\"Name\"]}')\n\n    return \"There are totally {} buckets.\".format(len(response['Buckets']))<\/pre>\n\n\n\n<p><strong>\u90e8\u7f72<\/strong><\/p>\n\n\n\n<p>\u73b0\u5728\u5c31\u53ef\u4ee5\u901a\u8fc7 CDK \u5c06 Lambda \u90e8\u7f72\u5230\u4e91\u7aef\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cdk deploy\n\n\u4f60\u4f1a\u770b\u5230\u7c7b\u4f3c\u5982\u4e0b\u8f93\u51fa\uff1a\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u2728  Synthesis time: 6.82s\n\nThis deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening).\nPlease confirm you intend to make the following modifications:\n\nIAM Statement Changes\n\u250c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502   \u2502 Resource                      \u2502 Effect \u2502 Action         \u2502 Principal                    \u2502 Condition \u2502\n\u251c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n\u2502 + \u2502 ${MyFunction\/ServiceRole.Arn} \u2502 Allow  \u2502 sts:AssumeRole \u2502 Service:lambda.amazonaws.com \u2502           \u2502\n\u2514\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\nIAM Policy Changes\n\u250c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502   \u2502 Resource                  \u2502 Managed Policy ARN                                                             \u2502\n\u251c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\n\u2502 + \u2502 ${MyFunction\/ServiceRole} \u2502 arn:${AWS::Partition}:iam::aws:policy\/service-role\/AWSLambdaBasicExecutionRole \u2502\n\u2514\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n(NOTE: There may be security-related changes not in this list. See https:\/\/github.com\/aws\/aws-cdk\/issues\/1299)\n\nDo you wish to deploy these changes (y\/n)? y\nIamRoleLambdaStack: deploying...\n[0%] start: Publishing f8f0b6e3c0970c2b86947588bc5ab7fe4031c3bd8ea32db737f41a96be64a01c:current_account-current_region\n[0%] start: Publishing a7505bba7bbf4039cb047dabb3f44c5254c20fae6434b5b2ddbff30e7dfbb009:current_account-current_region\n[50%] success: Published f8f0b6e3c0970c2b86947588bc5ab7fe4031c3bd8ea32db737f41a96be64a01c:current_account-current_region\n[100%] success: Published a7505bba7bbf4039cb047dabb3f44c5254c20fae6434b5b2ddbff30e7dfbb009:current_account-current_region\nIamRoleLambdaStack: creating CloudFormation changeset...\n\n \u2705  IamRoleLambdaStack\n\n\u2728  Deployment time: 58.14s\n\nStack ARN:\narn:aws:cloudformation:us-east-1:376200971131:stack\/IamRoleLambdaStack\/63a27650-e479-11ec-9052-12b9e23c7aa5\n\n\u2728  Total time: 64.96s<\/pre>\n\n\n\n<p>\u73b0\u5728\u6253\u5f00 AWS Console \u7f51\u9875\uff0c\u5728 Lambda \u9875\u9762\u7684 Test \u9009\u9879\u5361\u70b9\u51fb Test \u6309\u94ae\u6267\u884c\u8be5 Lambda\u3002\u7531\u4e8e\u6211\u4eec\u8fd8\u6ca1\u6709\u7ed9 Lambda \u8d4b\u4e88\u8bfb\u53d6 S3 bucket \u7684\u6743\u9650\uff0c\u56e0\u6b64 Lambda \u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">START RequestId: 6fd00bc4-2630-4cf4-ad2e-4b261142f943 Version: $LATEST\n[ERROR] ClientError: An error occurred (AccessDenied) when calling the ListBuckets operation: Access Denied\nTraceback (most recent call last):\n  File \"\/var\/task\/app.py\", line 6, in lambda_handler\n    response = s3.list_buckets()\n  File \"\/var\/runtime\/botocore\/client.py\", line 391, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n  File \"\/var\/runtime\/botocore\/client.py\", line 719, in _make_api_call\n    raise error_class(parsed_response, operation_name)END RequestId: 6fd00bc4-2630-4cf4-ad2e-4b261142f943\nREPORT RequestId: 6fd00bc4-2630-4cf4-ad2e-4b261142f943  Duration: 1738.98 ms    Billed Duration: 1739 ms    Memory Size: 128 MB Max Memory Used: 71 MB  Init Duration: 260.31 ms<\/pre>\n\n\n\n<p><strong>\u521b\u5efa\u89d2\u8272\u5e76\u914d\u7f6e\u6743\u9650<\/strong><\/p>\n\n\n\n<p>\u4e3a\u4e86\u8ba9 Lambda \u80fd\u591f\u8bfb\u53d6 S3 bucket\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u89d2\u8272\uff0c\u5e76\u4e3a\u8be5\u89d2\u8272\u914d\u7f6e\u5141\u8bb8\u8bbf\u95ee S3 bucket \u7684\u7b56\u7565\u3002\u7f16\u8f91 <strong>.\/lib\/iam-role-lambda-stack.ts<\/strong> \u4e3a\u5982\u4e0b\u5185\u5bb9\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import { Stack, StackProps } from 'aws-cdk-lib';\nimport { Construct } from 'constructs';\nimport * as lambda from 'aws-cdk-lib\/aws-lambda';\nimport * as iam from 'aws-cdk-lib\/aws-iam';\n\nexport class IamRoleLambdaStack extends Stack {\n  constructor(scope: Construct, id: string, props?: StackProps) {\n    super(scope, id, props);\n\n    const myRole = new iam.Role(this, 'MyRole', {\n      assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),\n    });\n    myRole.addToPolicy(new iam.PolicyStatement({\n      effect: iam.Effect.ALLOW,\n      resources: ['arn:aws:s3:::*'],\n      actions: ['s3:ListAllMyBuckets'],\n    }));\n\n    new lambda.Function(this, 'MyFunction', {\n      runtime: lambda.Runtime.PYTHON_3_9,\n      handler: 'app.lambda_handler',\n      code: lambda.Code.fromAsset('.\/my_function'),\n      role: myRole,\n    });\n  }\n}\n<\/pre>\n\n\n\n<p>\u5728\u4e0a\u8fb9\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a <strong>myRole<\/strong> \u7684\u89d2\u8272\uff0c\u5e76\u4e14\u4e3a\u8be5\u89d2\u8272\u6dfb\u52a0\u4e86\u4e00\u4e2a\u7b56\u7565\u3002\u8be5\u7b56\u7565\u5141\u8bb8\u5bf9\u6240\u6709 S3 \u8d44\u6e90\u6267\u884c <strong>s3:ListAllMyBuckets<\/strong> \u64cd\u4f5c\u3002\u6700\u540e\u5c06\u8be5\u89d2\u8272\u5173\u8054\u81f3 Lambda\uff0c\u4f7f\u8be5 Lambda \u5177\u6709\u76f8\u5e94\u6743\u9650\u3002<\/p>\n\n\n\n<p>\u518d\u6b21\u6267\u884c <strong>cdk deploy<\/strong> \u90e8\u7f72\uff0c\u7136\u540e\u4ee5\u540c\u6837\u7684\u65b9\u6cd5\u518d\u6b21\u6267\u884c\u8be5 Lambda\u3002\u8fd9\u6b21 Lambda \u6210\u529f\u8bfb\u53d6\u4e86\u6211\u7684\u6240\u6709 S3 bucket\uff0c\u5e76\u5728\u7ed3\u679c\u4e2d\u8fd4\u56de\u4e86 bucket \u7684\u6570\u91cf\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">There are totally 57 buckets.<\/pre>\n\n\n\n<p>\u6211\u4eec\u7684 Python \u4ee3\u7801\u8fd8\u5728\u65e5\u5fd7\u4e2d\u8bb0\u5f55\u4e86\u6240\u6709 S3 bucket \u7684\u540d\u5b57\u3002\u7531\u4e8e\u6211\u7684 bucket \u6570\u91cf\u8fc7\u591a\uff0c\u56e0\u6b64\u6211\u53ea\u5c55\u793a\u90e8\u5206\u65e5\u5fd7\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">START RequestId: c3eb252b-246c-46c6-a715-ddc723c8d075 Version: $LATEST\nExisting buckets:\ncdk-hnb659fds-assets-376200971131-us-east-1\ncdk-hnb659fds-assets-376200971131-us-west-2\n...\nEND RequestId: c3eb252b-246c-46c6-a715-ddc723c8d075\nREPORT RequestId: c3eb252b-246c-46c6-a715-ddc723c8d075  Duration: 1733.98 ms    Billed Duration: 1734 ms    Memory Size: 128 MB Max Memory Used: 71 MB  Init Duration: 236.72 ms<\/pre>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86 IAM \u4e09\u8981\u7d20\u4ee5\u53ca\u7b56\u7565\uff0c\u5e76\u901a\u8fc7\u5b9e\u6218\u6f14\u7ec3\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u57fa\u4e8e\u89d2\u8272\u7684\u6743\u9650\u63a7\u5236\u8d4b\u4e88 Lambda \u76f8\u5173\u6743\u9650\u3002\u4e0d\u4ec5\u662f Lambda\uff0cAWS \u4e2d\u7edd\u5927\u591a\u6570\u7684\u670d\u52a1\u90fd\u53ef\u4ee5\u901a\u8fc7\u89d2\u8272\u914d\u7f6e\u6743\u9650\uff0c\u4f8b\u5982 ECS\uff0cRedshift \u7b49\u3002\u8bfb\u8005\u53ef\u5173\u6ce8\u5404\u4e2a\u670d\u52a1\u4e2d\u7684\u89d2\u8272\u9009\u9879\u81ea\u884c\u914d\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 IAM \u5168\u79f0 Identity and Access Management\uff0c\u4e2d\u6587\u7ffb\u8bd1\u4e3a\u8eab\u4efd\u548c\u63a7\u5236\u7ba1\u7406\uff0c\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,9],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10084"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10084"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10084\/revisions"}],"predecessor-version":[{"id":10090,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/10084\/revisions\/10090"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}