Lambda Access Rights
In this lesson, you will modify your application code to enable the access rights of Lambda functions.
We'll cover the following
Security requirements #
AWS does not trust a Lambda function to access a database or an S3 bucket just because they belong to the same account. You need to explicitly allow the use of each external resource from a Lambda function. To do that, you’ll need to modify the IAM policy associated with a function.
Your function currently has two actions:
- displaying a form
- processing the form
The form processing action will need access to an S3 bucket, but the form display action does not need any specific security access. When two different actions need different security levels, it’s usually a good time to start thinking about breaking them into different Lambda functions.
With container-based applications, an API server process usually needs a superset of all the security permissions for all the contained actions. That makes setup and deployment easier, but it also means that a small security bug can easily turn into a disaster. If intruders break through a gatekeeper process, they can easily access all back-end resources. With Lambda functions, separating actions according to their security needs becomes very easy. Each function can have specific access to only the necessary resources. If a third-party dependency introduces a security issue in one of your API endpoints, the keys to the kingdom are still locked in a safe.
In Chapter 6, you created a new API endpoint to handle POST requests and just wired it up to the same function as the GET handler. Let’s split the two endpoints into two functions so you can manage security better (see the figure below).
ShowFormFunction
#
You can use this opportunity for some nice housekeeping. Let’s do the following:
- Rename the old Lambda function from
HelloWorld
toShowFormFunction
, so its purpose becomes clearer. - Rename the function code directory on your disk from
hello-world
to something more meaningful, such asuser-form
, and update theCodeUri
property of the function accordingly. - Rename
app.js
inside the project code directory to something more meaningful, for example,show-form.js
, and update theHandler
property of the function accordingly. - Rename the first event mapping to something meaningful, for example,
ShowForm
. - Finally, instead of
/hello
as the API resource path, just use the root resource (/
).
Get hands-on with 1400+ tech skills courses.