{"id":9957,"date":"2023-03-17T11:06:33","date_gmt":"2023-03-17T03:06:33","guid":{"rendered":"http:\/\/123.57.164.21\/?p=9957"},"modified":"2023-03-17T11:06:33","modified_gmt":"2023-03-17T03:06:33","slug":"grant-aws-lambda-access-to-secrets-manager","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=9957","title":{"rendered":"Grant AWS Lambda Access to Secrets Manager"},"content":{"rendered":"\n<p><strong>In order to grant a Lambda function access to Secrets Manager, we have to attach an IAM policy to the function&#8217;s execution role. The policy should grant permissions for all the <code>Actions<\/code> the function needs to perform on the secrets.<\/strong><\/p>\n\n\n\n<p>For example, the following policy grants permissions for the most commonly used secrets manager actions on a specific secret.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The policy applies to a specific secret, therefore make sure to replace the YOUR_SECRET_ARN placeholder in the Resource element with the secret's ARN.\nYou can specify multiple values if the lambda function needs access to multiple secrets.\n<\/pre>\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\/\u56fe\u7247-89-1018x1024.png\" alt=\"\" class=\"wp-image-9959\" width=\"465\" height=\"467\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-89-1018x1024.png 1018w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-89-298x300.png 298w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-89-150x150.png 150w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-89-768x773.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-89-830x835.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-89-230x231.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-89-350x352.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-89-480x483.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-89.png 1294w\" sizes=\"(max-width: 465px) 100vw, 465px\" \/><\/figure><\/div>\n\n\n\n<p>If your lambda function only needs to read a secret, you only need the <code>secretsmanager:GetSecretValue<\/code> action.<\/p>\n\n\n\n<p>The actions your lambda function needs to perform on the secrets are use case specific.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">You could set \"secretsmanager:*\" for the Action element in the policy to grant full secrets manager access to the lambda function. However, it's best practice to grant an entity the least permissions that get the job done.<\/pre>\n\n\n\n<p>You can view a full list of the secrets manager <code>Actions<\/code> in the <a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/docs.aws.amazon.com\/service-authorization\/latest\/reference\/list_awssecretsmanager.html#awssecretsmanager-actions-as-permissions\">Secrets Manager Actions table<\/a>.<\/p>\n\n\n\n<p>There is a <code>Description<\/code> column that explains what each action does.<\/p>\n\n\n\n<p>To attach a policy to the lambda function&#8217;s execution role:<\/p>\n\n\n\n<ul><li>Open the AWS Lambda console and click on your function&#8217;s name<\/li><li>Click on the <code>Configuration<\/code> tab and then click <code>Permissions<\/code><\/li><\/ul>\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\/\u56fe\u7247-90-1024x486.png\" alt=\"\" class=\"wp-image-9960\" width=\"530\" height=\"251\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-90-1024x486.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-90-300x142.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-90-768x365.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-90-830x394.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-90-230x109.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-90-350x166.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-90-480x228.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-90.png 1314w\" sizes=\"(max-width: 530px) 100vw, 530px\" \/><\/figure><\/div>\n\n\n\n<ul><li>Click on the function&#8217;s role<\/li><\/ul>\n\n\n\n<ul><li>Click on <code>Add permissions<\/code> and then click <code>Create inline policy<\/code><\/li><\/ul>\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\/\u56fe\u7247-91-1024x456.png\" alt=\"\" class=\"wp-image-9961\" width=\"524\" height=\"233\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-91-1024x456.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-91-300x134.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-91-768x342.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-91-830x370.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-91-230x102.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-91-350x156.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-91-480x214.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-91.png 1280w\" sizes=\"(max-width: 524px) 100vw, 524px\" \/><\/figure><\/div>\n\n\n\n<ul><li>In the <code>JSON<\/code> editor, paste the following policy.<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">Replace the YOUR_SECRET_ARN placeholder and adjust the Actions your lambda function needs to execute.<\/pre>\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\/\u56fe\u7247-92-1022x1024.png\" alt=\"\" class=\"wp-image-9962\" width=\"532\" height=\"532\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-92-1022x1024.png 1022w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-92-300x300.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-92-150x150.png 150w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-92-768x769.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-92-830x831.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-92-230x230.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-92-350x351.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-92-480x481.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/03\/\u56fe\u7247-92.png 1296w\" sizes=\"(max-width: 532px) 100vw, 532px\" \/><\/figure><\/div>\n\n\n\n<ul><li>Click <code>Review Policy<\/code> and give your policy a name, then click <code>Create policy<\/code><\/li><\/ul>\n\n\n\n<p>At this point, the lambda function&#8217;s role has been extended with a policy that grants access to some secrets manager actions on a specific secret.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">It can take up to a minute until the IAM changes have been propagated and the policy is in effect.<\/pre>\n\n\n\n<p>lambda typescript sample<\/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 { MongoClient } from 'mongodb';\nimport { Axios } from 'axios';\nimport AWS from 'aws-sdk';\nimport {\n    SecretsManagerClient,\n    GetSecretValueCommand,\n  } from \"@aws-sdk\/client-secrets-manager\";\n\nconst secret_name = \"cs-xxxxxx\";\n\nconst client = new SecretsManagerClient({\n  region: \"ap-northeast-1\",\n});\n\nexport const handler = async (event) => {\n    \n    let response;\n\n    try {\n        response = await client.send(\n          new GetSecretValueCommand({\n            SecretId: secret_name,\n            VersionStage: \"AWSCURRENT\", \/\/ VersionStage defaults to AWSCURRENT if unspecified\n          })\n        );\n      } catch (error) {\n        \/\/ For a list of exceptions thrown, see\n        \/\/ https:\/\/docs.aws.amazon.com\/secretsmanager\/latest\/apireference\/API_GetSecretValue.html\n        throw error;\n      }\n      \n    const secret = response.SecretString;\n    console.log('******lambda tets start******');\n    console.log(event);\n    console.log('Axios:' + Axios.toString);\n    console.log('MongoClient:' + MongoClient.name);\n    console.log('S3:' + AWS.S3.toString);\n    console.log('******lambda test end******');\n};<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In order to grant a Lambda function access to Secrets M [&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\/9957"}],"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=9957"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9957\/revisions"}],"predecessor-version":[{"id":9964,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9957\/revisions\/9964"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}