{"id":935,"date":"2020-12-09T12:23:49","date_gmt":"2020-12-09T03:23:49","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/?p=935"},"modified":"2021-01-19T11:49:01","modified_gmt":"2021-01-19T02:49:01","slug":"deactivate-aws-access-key","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2020\/12\/09\/deactivate-aws-access-key\/","title":{"rendered":"AWS\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u3092\u81ea\u52d5\u7684\u306b\u7121\u52b9\u5316\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"
\u897f\u85e4\u3067\u3059\u3002<\/p>\n
\u307f\u306a\u3055\u3093\u306fAWS\u306e\u5404\u7a2e\u30b5\u30fc\u30d3\u30b9\u3092\u4f7f\u3063\u305f\u958b\u767a\u3092\u884c\u3046\u969b\u3001AWS\u306e\u5404\u7a2e\u64cd\u4f5c\u306e\u6a29\u9650\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306e\u300c\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u300d\u306e\u53d6\u308a\u6271\u3044\u306f\u3069\u306e\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u304b\uff1f<\/p>\n
\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\uff08\u3068\u30b7\u30fc\u30af\u30ec\u30c3\u30c8\u30ad\u30fc\uff09\u306f\u6f0f\u6d29\u3057\u305f\u5834\u5408\u3001\u4e0d\u6b63\u306bAWS\u30ea\u30bd\u30fc\u30b9\u3092\u4f5c\u6210\u30fb\u5229\u7528\u3055\u308c\u3066\u3057\u307e\u3046\u6050\u308c\u306a\u3069\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n
\u305d\u306e\u305f\u3081\u3001\u4e0d\u6b63\u5229\u7528\u3092\u9632\u3050\u305f\u3081\u306b<\/p>\n
\u306a\u3069\u306e\u5bfe\u7b56\u304c\u8003\u3048\u3089\u308c\u307e\u3059\u3002<\/p>\n
\u3057\u304b\u3057\u3001\u300c\u500b\u4eba\u7684\u306b\u4f55\u304bAWS\u3092\u4f7f\u3063\u3066\u691c\u8a3c\u3057\u305f\u3044\u300d\u3068\u3044\u3046\u3088\u3046\u306a\u3068\u304d\u306b\u306f\u300c\u6a29\u9650\u3092\u6700\u5c0f\u9650\u306b\u7d5e\u3063\u3066\u2026.\u300d\u306a\u3069\u306e\u8abf\u6574\u306f\u5f8c\u56de\u3057\u306b\u3057\u3066\u3057\u307e\u3044\u304c\u3061\u3067\u3059\u3002
\n\u300cAWS\u3092\u4f7f\u3063\u305f\u691c\u8a3c\u300d\u304c\u76ee\u7684\u306a\u306e\u3067\u3059\u304b\u3089\u3001\u300c\u6a29\u9650\u306e\u8abf\u6574\u300d\u306b\u6642\u9593\u3092\u53d6\u3089\u308c\u305f\u304f\u306f\u3042\u308a\u307e\u305b\u3093\u3088\u306d\u3002
\n\u3068\u306f\u3044\u3048\u3001\u4e0d\u6b63\u5229\u7528\u3055\u308c\u306a\u3044\u305f\u3081\u306e\u306a\u3093\u3089\u304b\u306e\u63aa\u7f6e\u306f\u8b1b\u3058\u3066\u304a\u304d\u305f\u3044\u3082\u306e\u2026.<\/p>\n
\u4eca\u56de\u306f\u305d\u3093\u306a\u958b\u767a\u8005\u306e\u65b9\u306b\u4f7f\u3063\u3066\u3044\u305f\u3060\u304d\u305f\u3044\u81ea\u52d5\u5316\u8a2d\u5b9a\u306e\u5b9f\u88c5\u4f8b\u3092\u7d39\u4ecb\u3044\u305f\u3057\u307e\u3059\u3002<\/p>\n
\u4eca\u56de\u306f\u6b21\u306e\u3088\u3046\u306a\u5834\u9762\u3092\u60f3\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n
\u4eee\u306b\u3053\u306eIAM\u30e6\u30fc\u30b6\u30fc\u540d\u3092 <\/p>\n \u305d\u3057\u3066\u3001\u4eca\u56de\u306f<\/p>\n \u3068\u3044\u3046\u4ed5\u7d44\u307f\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002 Lambda\u95a2\u6570\u306fPython\u3067\u5b9f\u88c5\u3057\u3066\u3001\u30b3\u30fc\u30c9\u306f\u6b21\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n handler.py<\/p>\n \u30b3\u30fc\u30c9\u5185\u306b\u306f\u74b0\u5883\u5909\u6570\u304c2\u3064\u3042\u308a\u307e\u3059\u304c\u3001\u305d\u308c\u305e\u308c \u305d\u3057\u3066\u3001\u3053\u306eLambda\u95a2\u6570\u304c\u5b9f\u884c\u3055\u308c\u308b\u3068\u30bf\u30fc\u30b2\u30c3\u30c8\u3068\u306a\u308b\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u304c\u7121\u52b9\u5316\u3055\u308c\u307e\u3059\u3002<\/p>\n \u6b21\u306b\u3001\u4e0a\u8a18\u306eLambda\u95a2\u6570\u3092\u6bce\u65e50:00\u306b\u81ea\u52d5\u5b9f\u884c\u3059\u308b\u8a2d\u5b9a\u3092\u884c\u306a\u3063\u3066\u3044\u304d\u307e\u3059\u3002 .env<\/p>\n serverless.yml<\/p>\n \u6ce8\u76ee\u3057\u3066\u3044\u305f\u3060\u304d\u305f\u3044\u306e\u306f\u3001\u6700\u5f8c\u3001\u6570\u884c\u5206\u306e\u90e8\u5206\u3067\u3059\u3002 \u3042\u3068\u3001Docker\u30d9\u30fc\u30b9\u3067\u30c7\u30d7\u30ed\u30a4\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002 Dockerfile<\/p>\n docker-compose.yml<\/p>\n \u4ee5\u4e0a\u3092\u8e0f\u307e\u3048\u3066\u3001\u30c7\u30d7\u30ed\u30a4\u3059\u308b\u3068\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n Lambda\u95a2\u6570 CloudWatch Events\u30eb\u30fc\u30eb JST 0:00\u306b\u306a\u308b\u3068\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u3092\u7121\u52b9\u5316\u3059\u308bLambda\u95a2\u6570\u304c\u5b9f\u884c\u3055\u308c\u308b\u3002\u3068\u8a00\u3046\u5f62\u3067\u3059\u3002<\/p>\n \u305d\u3057\u3066\u3001\u3053\u306e\u8a2d\u5b9a\u3092\u7d42\u3048\u3066\u3001Lambda\u5b9f\u884c\u30ed\u30b0\u3092\u898b\u307e\u3059\u3068<\/p>\n <\/p>\n \u3068\u8a00\u3046\u3088\u3046\u306b\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u306e\u7121\u52b9\u5316\u304c\u5b9f\u884c\u3055\u308c\u305f\u65e8\u8868\u793a\u3055\u308c\u3066\u304a\u308a\u3001\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u306e\u3042\u308bIAM\u30e6\u30fc\u30b6\u30fc\u306e\u60c5\u5831\u3092\u898b\u306b\u3044\u304f\u3068<\/p>\n <\/p>\n \u3061\u3083\u3093\u3068\u3001\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u304c\u7121\u52b9\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n \u4ee5\u4e0a\u3067AWS\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u3092\u81ea\u52d5\u7684\u306b\u7121\u52b9\u5316\u3059\u308b\u5b9f\u88c5\u306e\u5b8c\u4e86\u3067\u3059\u3002<\/p>\n \u3053\u306e\u3088\u3046\u306b\u3057\u3066\u304a\u3051\u3070\u3001\u4eee\u306bAWS\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u306b\u5f37\u529b\u306a\u6a29\u9650\u3092\u4ed8\u4e0e\u3057\u305f\u307e\u307e\u6b8b\u3057\u3066\u3057\u307e\u3063\u3066\u3044\u3066\u3082\u3001\u5c11\u306a\u304f\u3068\u3082\u6bce\u65e50:00\u306b\u306a\u3063\u305f\u3089\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u81ea\u4f53\u304c\u7121\u52b9\u5316\u3055\u308c\u308b\u306e\u3067\u3001\u300c\u5f37\u3044\u6a29\u9650\u4ed8\u4e0e\u3057\u305f\u72b6\u614b\u3092\u5fd8\u308c\u305f\u307e\u307e\u653e\u7f6e\u3057\u3066\u3057\u307e\u3063\u3066\u3044\u305f\u300d\u3068\u8a00\u3046\u3088\u3046\u306a\u5834\u9762\u306f\u56de\u907f\u3067\u304d\u307e\u3059\u3002<\/p>\n \u672c\u6765\u306f\u6700\u5c0f\u6a29\u9650\u306e\u539f\u5247\u306b\u5247\u3063\u3066\u5fc5\u8981\u306a\u6a29\u9650\u3092\u5fc5\u8981\u306a\u3060\u3051\u4ed8\u4e0e\u3059\u308b\u3001\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u306e\u6f0f\u6d29\u306b\u6ce8\u610f\u3057\u30ad\u30fc\u81ea\u4f53\u3082\u5b9a\u671f\u7684\u306a\u30ed\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u884c\u3046\u306a\u3069\u3001IAM\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u306b\u6cbf\u3063\u305f\u904b\u7528\u3092\u884c\u3046\u3053\u3068\u304c\u5927\u539f\u5247\u3067\u306f\u3042\u308a\u307e\u3059\u304c\u3001\u5b89\u5168\u306b\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u3092\u4f7f\u3046\u305f\u3081\u306e1\u3064\u306e\u30a2\u30a4\u30c7\u30a3\u30a2\u3068\u3057\u3066\u53c2\u8003\u306b\u306a\u308c\u3070\u5e78\u3044\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":" \u306f\u3058\u3081\u306b \u897f\u85e4\u3067\u3059\u3002 \u307f\u306a\u3055\u3093\u306fAWS\u306e\u5404\u7a2e\u30b5\u30fc\u30d3\u30b9\u3092\u4f7f\u3063\u305f\u958b\u767a\u3092\u884c\u3046\u969b\u3001AWS\u306e\u5404\u7a2e\u64cd\u4f5c\u306e\u6a29\u9650\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306e\u300c\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u300d\u306e\u53d6\u308a\u6271\u3044\u306f\u3069\u306e\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u304b\uff1f \u30a2\u30af\u30bb\u30b9\u30ad\u30fc\uff08\u3068\u30b7\u30fc\u30af\u30ec\u30c3\u30c8\u30ad\u30fc\uff09\u306f\u6f0f\u6d29\u3057\u305f\u5834\u5408\u3001\u4e0d\u6b63\u306bAWS\u30ea\u30bd\u30fc\u30b9\u3092\u4f5c\u6210\u30fb\u5229\u7528\u3055\u308c\u3066\u3057\u307e\u3046\u6050\u308c\u306a\u3069\u304c\u3042\u308a\u307e\u3059\u3002 \u305d\u306e\u305f\u3081\u3001\u4e0d\u6b63\u5229\u7528\u3092\u9632\u3050\u305f\u3081\u306b \u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u3092\u7528\u3044\u305a\u3001\u300c\u30ed\u30fc\u30eb\u300d\u30d9\u30fc\u30b9\u3067\u306e\u6a29\u9650\u4ed8\u4e0e \u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u81ea\u4f53\u306b\u4ed8\u4e0e\u3059\u308b\u6a29\u9650\u3092\u5fc5 […]<\/p>\n","protected":false},"author":6,"featured_media":936,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/935"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=935"}],"version-history":[{"count":2,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/935\/revisions"}],"predecessor-version":[{"id":1098,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/935\/revisions\/1098"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media\/936"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}access-key-user<\/code>\u3068\u3057\u307e\u3057\u3087\u3046\u3002
\nAWS\u30de\u30cd\u30b8\u30e1\u30f3\u30c8\u30b3\u30f3\u30bd\u30fc\u30eb\u306e\u753b\u9762\u3067\u8a00\u3046\u3068\u3053\u3093\u306a\u611f\u3058\u3067\u3057\u3087\u3046\u304b<\/p>\n\n
access-key-user<\/code>\u306e\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u3092\u7121\u52b9\u5316\u3059\u308b\u3002<\/li>\n<\/ul>\n
\n\uff08\u201dServerless Framework\u201d\u3092\u4f7f\u7528\u3057\u307e\u3059\u304c\u3001\u30c7\u30d7\u30ed\u30a4\u81ea\u4f53\u306e\u89e3\u8aac\u306f\u5272\u611b\u3044\u305f\u3057\u307e\u3059\u3002\uff09<\/p>\n\u5b9f\u88c5\u7528\u306e\u30b3\u30fc\u30c9\u5185\u5bb9<\/h2>\n
Lambda\u95a2\u6570<\/h3>\n
import json\nimport os\nimport boto3\n\ndef deactivate_access_key(access_key):\n responce = access_key.deactivate()\n print(responce)\n HTTPStatusCode = responce['ResponseMetadata']['HTTPStatusCode']\n print("HTTPStatusCode:")\n print(HTTPStatusCode)\n accessKeyId = access_key.access_key_id\n print(accessKeyId + " is deactivated.")\n return\n\ndef main(event, context):\n # \u74b0\u5883\u5909\u6570\u304b\u3089\u53d6\u5f97\n targetAccessKeyUserName = os.environ['TARGET_ACCESS_KEY_USER_NAME']\n targetAccessKeyId = os.environ['TARGET_ACCESS_KEY_ID']\n # IAM\u306e\u30ea\u30bd\u30fc\u30b9\u4f5c\u6210\n iamResource = boto3.resource('iam')\n access_key = iamResource.AccessKey(targetAccessKeyUserName, targetAccessKeyId)\n # \u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u306e\u7121\u52b9\u5316\n deactivate_access_key(access_key)\n return\n<\/code><\/pre>\n
\nTARGET_ACCESS_KEY_USER_NAME<\/code>\u306f\u7121\u52b9\u5316\u3092\u884c\u3044\u305f\u3044\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u304c\u7d10\u3065\u304fIAM\u30e6\u30fc\u30b6\u30fc\u540d\uff08\u4eca\u56de\u306e\u4f8b\u3067\u306f
access-key-user<\/code>\uff09
\nTARGET_ACCESS_KEY_ID<\/code>\u306f\u7121\u52b9\u5316\u3092\u884c\u3044\u305f\u3044\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\uff08
AKIAIOSFODNN7EXAMPLE<\/code>\u306e\u3088\u3046\u306a\u82f1\u6570\u5b57\u3067\u8868\u3055\u308c\u308b\uff09
\n\u3092\u9069\u7528\u3057\u307e\u3059\u3002
\n\u203b\u3082\u3057\u3001Serverless Framework\u3092\u4f7f\u308f\u306a\u3044\u5834\u5408\u306fLambda\u306e\u74b0\u5883\u5909\u6570\u8a2d\u5b9a\u306b\u3066\u8a18\u8f09\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\nServerless Framework\u306e\u5b9f\u88c5\u6e96\u5099<\/h3>\n
\nServerless Framework\u3067\u306e\u5b9f\u88c5\u3067\u4f7f\u3046.env<\/code>\u30d5\u30a1\u30a4\u30eb\u3068
serverless.yml<\/code>\u30d5\u30a1\u30a4\u30eb\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n
AWS_ACCESS_KEY_ID=<Serverless Framework\u3067\u30c7\u30d7\u30ed\u30a4\u3092\u884c\u3046\u7528\u306e\u30a2\u30af\u30bb\u30b9\u30ad\u30fc>\nAWS_SECRET_ACCESS_KEY=<Serverless Framework\u3067\u30c7\u30d7\u30ed\u30a4\u3092\u884c\u3046\u7528\u306e\u30b7\u30fc\u30af\u30ec\u30c3\u30c8\u30ad\u30fc>\nTARGET_ACCESS_KEY_USER_NAME=access-key-user\nTARGET_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE<\/code><\/pre>\n
service: deactivate-access-key\n\nplugins:\n - serverless-dotenv-plugin\n - serverless-prune-plugin\n\ncustom:\n dotenv:\n include:\n - TARGET_ACCESS_KEY_USER_NAME\n - TARGET_ACCESS_KEY_ID\n prune:\n automatic: true\n number: 1\n\npackage:\n exclude:\n - .\/**\n include:\n - .\/handler.py\n\nprovider:\n name: aws\n runtime: python3.8\n region: ap-northeast-1\n stage: dev\n memorySize: 128\n iamRoleStatements:\n - Effect: Allow\n Action:\n - "iam:UpdateAccessKey"\n Resource:\n - "*"\n\nfunctions:\n deactivate-access-key:\n description: \u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u306e\u7121\u52b9\u5316\n name: deactivate-access-key\n handler: handler.main\n events:\n - schedule:\n name: deactivate-access-key\n description: JST 0:00\u306b\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u3092\u7121\u52b9\u5316\u3059\u308b\n rate: cron(00 15 * * ? *)\n<\/code><\/pre>\n
\ncron\u3067\u306e\u6307\u5b9a\u304c\u3042\u308a\u307e\u3059\u304c\u3053\u308c\u306b\u3088\u308a\u4e0a\u8a18\u306eLambda\u95a2\u6570\u304c\u6bce\u65e5JST 0:00\u306b\u5b9f\u884c\u3055\u308c\u308b\u3088\u3046\u306bCloudWatch Events\u30eb\u30fc\u30eb\u306e\u8a2d\u5b9a\u304c\u4ed8\u304d\u307e\u3059\u306e\u3067\u3001
\n\u300c\u591c\u306b\u306a\u3063\u305f\u3089AWS\u30a2\u30af\u30bb\u30b9\u30ad\u30fc\u3092\u81ea\u52d5\u7684\u306b\u7121\u52b9\u5316\u300d\u304c\u5b9f\u73fe\u3057\u307e\u3059\u3002<\/p>\n
\nDockerfile<\/code>\u3068
docker-compose.yml<\/code>\u306f\u6b21\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n
FROM node:15.3.0-alpine\n\nRUN npm update -g\n\n# install serverless framework\nRUN npm install -g serverless\n# install serverless plugins\nRUN npm i -D serverless-dotenv-plugin\nRUN npm install --save-dev serverless-prune-plugin\n\n# change work directory\nWORKDIR \/app\n\nCOPY . \/app\/<\/code><\/pre>\n
version: "3.8"\nservices:\n serverless:\n build:\n context: .\n volumes:\n - .:\/app\n env_file:\n - .env<\/code><\/pre>\n
\u5b9f\u88c5\u7d50\u679c<\/h2>\n
\n<\/p>\n
\n<\/p>\n\u307e\u3068\u3081<\/h2>\n