How to setup Security and compliance considerations when using AWS Lambda (Part 1)
Published: 2023-02-06 12:49:45

<!-- wp:paragraph -->
<p>AWS Lambda is a serverless computing service that enables you to run code without provisioning or managing servers. As with any other service, there are security and compliance considerations that you should take into account when using Lambda.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Some key security and compliance considerations when using Lambda include:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Identity and access management (IAM): Use IAM roles to control access to your Lambda functions and the resources they access. This will ensure that your Lambda functions can only access the resources they need to, and nothing more.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Encryption: Use encryption to protect the data and configuration of your Lambda functions and the resources they access. This will ensure that your data is protected in transit and at rest.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Network security: Use security groups and network ACLs to control access to your Lambda functions and the resources they access. This will ensure that only authorized traffic can reach your functions and resources.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Auditing and monitoring: Use CloudTrail, CloudWatch, and other logging and monitoring tools to track activity in your Lambda environment. This will help you detect and respond to security incidents and compliance violations.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Compliance: AWS Lambda supports a number of compliance standards, such as SOC 2, PCI DSS, and HIPAA. Ensure that you follow the relevant compliance requirements when using Lambda.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>When setting up security and compliance considerations for AWS Lambda, there are several key steps to take:</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Use IAM roles for Lambda execution</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Create an IAM role for your Lambda function that has the appropriate permissions to access other AWS resources. This will ensure that your Lambda function can only access the resources it needs to, and nothing more.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Using Management Console</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Here are the steps to create an IAM role for your Lambda function and attach the appropriate permissions to it:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Go to the IAM console in the AWS Management Console.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Click on the "Roles" menu and then click on the "Create role" button.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Select "Lambda" as the service that will use this role.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Attach the policies that you want the role to have. You can either attach managed policies or create custom policies.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Once you have finished attaching policies, review the role and give it a name and description.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Click on the "Create role" button to create the role.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Once the role is created, you can go to the Lambda function's configuration page and attach the role to the function.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>When creating or updating the Lambda function, you can specify the IAM role to be used by the function.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>To add permissions to the role, you can update the policies attached to the role, or create new policies and attach them to the role.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>To ensure that your Lambda function can only access the resources it needs to, you should also implement resource-based policies in your S3 bucket and other resources, to restrict access to the files stored in the bucket and other resources, to only the IAM role attached to your Lambda function.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Regularly review and audit the policies and permissions attached to the role to ensure that the role has only the required permissions and no more.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:heading {"level":3} -->
<h3>Using AWS CLI </h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Here are the steps to create an IAM role for your Lambda function and attach the appropriate permissions to it using the AWS CLI:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Install and configure the AWS CLI on your local machine.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Run the following command to create a new IAM role</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code><code>aws iam create-role --role-name my-lambda-role --assume-role-policy-document file://path/to/assume_role_policy.json</code></code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>  3. Attach the policies that you want the role to have. You can either attach managed policies or create custom policies.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code><code>aws iam attach-role-policy --role-name my-lambda-role --policy-arn arn:aws:iam::aws:policy/AWSLambdaFullAccess</code></code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>  4. Once you have finished attaching policies, you can also add permissions to the role by creating an inline policy and attaching it to the role. </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code><code>aws iam put-role-policy --role-name my-lambda-role --policy-name my-inline-policy --policy-document file://path/to/policy.json</code></code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>  5. To ensure that your Lambda function can only access the resources it needs to, you should also implement resource-based policies in your S3 bucket and other resources, to restrict access to the files stored in the bucket and other resources, to only the IAM role attached to your Lambda function.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>  6. Once the role is created, you can use the following command to update the Lambda function's configuration, and attach the role to the function. </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code><code>aws lambda update-function-configuration --function-name my-function --role arn:aws:iam::account-id:role/my-lambda-role</code></code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>  7. Regularly review and audit the policies and permissions attached to the role to ensure that the role has only the required permissions and no more</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code><code>aws iam list-attached-role-policies --role-name my-lambda-role</code></code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Please note that the above commands are examples, you should replace the placeholders with the actual values for your specific use case.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Use VPCs to isolate Lambda functions</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Create a VPC for your Lambda function and configure it to only allow traffic from specific IP ranges or other VPCs. This will help to isolate your Lambda function and limit its exposure to the internet.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Using Management Console</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>You can use the following steps to create a VPC for your Lambda function and configure it to only allow traffic from specific IP ranges or other VPCs using the AWS Management Console:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Open the AWS Management Console and navigate to the Amazon VPC dashboard.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Click on the "Create VPC" button to create a new VPC.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Give the VPC a name and select the appropriate IPv4 CIDR block for your VPC. You can also specify the tenancy of the VPC, whether it should be a public or private VPC, and the Amazon VPCs that it should be connected to, if any.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Create the VPC by clicking on the "Create" button.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Once the VPC is created, navigate to the "Subnets" section of the VPC dashboard and create a new subnet in the VPC.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Give the subnet a name and select the appropriate IPv4 CIDR block for the subnet. You should also select the VPC that the subnet should be associated with.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Create the subnet by clicking on the "Create" button.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Navigate to the "Security Groups" section of the VPC dashboard and create a new security group.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Give the security group a name and select the VPC that the security group should be associated with.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>In the "Inbound" tab, add the appropriate IP ranges or other VPCs that should be able to access the security group.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Create the security group by clicking on the "Create" button.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Navigate to the Lambda dashboard and create a new Lambda function or edit an existing one.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>In the "Function code" section of the function configuration, select the subnet that you created earlier for the VPC.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>In the "Network" section of the function configuration, select the security group that you created earlier for the VPC.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Save the function configuration by clicking on the "Save" button.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>By following these steps, you will have created a VPC for your Lambda function and configured it to only allow traffic from specific IP ranges or other VPCs, helping to isolate the function and limit its exposure to the internet.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Using Aws CLI</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Here are the steps to create a VPC for your Lambda function and configure it to only allow traffic from specific IP ranges or other VPCs using the AWS CLI:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Open a terminal or command prompt and make sure that you have the AWS CLI installed and configured.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Create a new VPC by running the following AWS CLI command:</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>aws ec2 create-vpc --cidr-block &lt;your-vpc-cidr-block&gt; --instance-tenancy &lt;tenancy-type&gt;
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Replace <code>&lt;your-vpc-cidr-block&gt;</code> with the appropriate IPv4 CIDR block for your VPC, and <code>&lt;tenancy-type&gt;</code> with the tenancy of the VPC (e.g. default or dedicated).</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true,"start":3} -->
<ol start="3"><!-- wp:list-item -->
<li>Record the VPC ID returned by the above command, as you will need it for the next step.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Create a new subnet in the VPC by running the following AWS CLI command:</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>aws ec2 create-subnet --vpc-id &lt;vpc-id&gt; --cidr-block &lt;your-subnet-cidr-block&gt;
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Replace <code>&lt;vpc-id&gt;</code> with the VPC ID from the previous step, and <code>&lt;your-subnet-cidr-block&gt;</code> with the appropriate IPv4 CIDR block for the subnet.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true,"start":5} -->
<ol start="5"><!-- wp:list-item -->
<li>Record the subnet ID returned by the above command, as you will need it for the next step.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Create a new security group by running the following AWS CLI command:</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>aws ec2 create-security-group --group-name &lt;your-security-group-name&gt; --description &lt;your-security-group-description&gt; --vpc-id &lt;vpc-id&gt;
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Replace <code>&lt;your-security-group-name&gt;</code> and <code>&lt;your-security-group-description&gt;</code> with the appropriate values for your security group, and <code>&lt;vpc-id&gt;</code> with the VPC ID from step 3.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true,"start":7} -->
<ol start="7"><!-- wp:list-item -->
<li>Record the security group ID returned by the above command, as you will need it for the next step.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Authorize incoming traffic to the security group by running the following AWS CLI command:</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>aws ec2 authorize-security-group-ingress --group-id &lt;security-group-id&gt; --protocol &lt;protocol&gt; --port &lt;port-range&gt; --cidr &lt;source-ip-range&gt;
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Replace <code>&lt;security-group-id&gt;</code> with the security group ID from step 6, <code>&lt;protocol&gt;</code> with the appropriate protocol (e.g. TCP or UDP), <code>&lt;port-range&gt;</code> with the appropriate port range (e.g. 80-80), and <code>&lt;source-ip-range&gt;</code> with the IP ranges or other VPCs that should be able to access the security group.</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true,"start":9} -->
<ol start="9"><!-- wp:list-item -->
<li>Create or update your Lambda function to run in the VPC by running the following AWS CLI command:</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:code -->
<pre class="wp-block-code"><code>aws lambda update-function-configuration --function-name &lt;function-name&gt; --vpc-config SubnetIds=&lt;subnet-id&gt;,SecurityGroupIds=&lt;security-group-id&gt;
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Replace <code>&lt;function-name&gt;</code> with the name of your Lambda function, <code>&lt;subnet-id&gt;</code> with the subnet ID from the VPC you want to use, and &lt;security-group-id&gt; with the ID of the security group you want to use. This will update the configuration of your Lambda function to run within the specified VPC and with the specified security group.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Note that this is just an example of how you can use the AWS CLI to update the configuration of your Lambda function. The exact steps and commands may vary based on your specific use case and environment.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Encrypt data at rest and in transit</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Use services such as AWS KMS and AWS CloudHSM to encrypt data at rest and in transit. This will help to protect your data from unauthorized access.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can follow this step-by-step guide to encrypt data at rest and in transit using AWS KMS and AWS CloudHSM:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Encrypting data at rest: a. Choose an encryption service: Login to the AWS Management Console and choose either AWS KMS or S3 server-side encryption. b. Create a key: In AWS KMS, create a customer master key (CMK) for data encryption. In S3, you can use the default encryption or create a new one. c. Encrypt your data: For S3, you can encrypt your data by enabling server-side encryption for a bucket or an object. For AWS KMS, you can encrypt data by calling the Encrypt API action. d. Store your data: Store your encrypted data in the chosen service.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Encrypting data in transit: a. Create a VPC: Login to the AWS Management Console and create a VPC. b. Create a VPN connection: Create a VPN connection within your VPC to encrypt data in transit. c. Enable encryption for VPC data transfer: Enable encryption for data transfer within your VPC using VPC endpoints or VPN connections. d. Enable SSL/TLS encryption: For data transfer over the internet, enable SSL/TLS encryption for your application.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Note: These are high-level steps and the actual steps might differ based on the encryption service chosen and the complexity of your infrastructure. Additionally, AWS CloudHSM is used for more complex encryption needs, such as HSM-protected keys for payment card industry (PCI) compliance.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Monitor and log Lambda function activity</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Use services such as AWS CloudTrail and AWS CloudWatch to monitor and log activity on your Lambda function. This will help you to detect and respond to security incidents quickly.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here is a step-by-step guide to monitor and log Lambda function activity using AWS CloudTrail and AWS CloudWatch:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Enable AWS CloudTrail: a. Login to the AWS Management Console. b. Go to the CloudTrail service. c. Create a new trail or use an existing one. d. Ensure that the trail is logging all AWS Lambda API calls.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Set up AWS CloudWatch Logs: a. Login to the AWS Management Console. b. Go to the CloudWatch service. c. Create a new log group for your Lambda function or use an existing one. d. Go to the Lambda function in the AWS Management Console. e. Go to the Monitoring section. f. Under Logs, choose the log group that you created in step c.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Monitor and respond to security incidents: a. View logs: In the CloudWatch Logs, you can view the log data generated by your Lambda function. b. Create CloudWatch Alarms: In CloudWatch, you can create alarms based on log metric filters to monitor specific log events and trigger actions when certain conditions are met. c. Respond to security incidents: When a security incident is detected, you can take appropriate action to resolve the issue and prevent future incidents.</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Note: These are high-level steps and the actual steps might differ based on the complexity of your infrastructure and the specific use case. Additionally, you can also integrate other security tools with AWS CloudTrail and AWS CloudWatch to further enhance your security and incident response capabilities.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Use AWS Lambda security best practice</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Follow the security best practices provided by AWS for Lambda, such as using resource-based policies for Lambda function and S3 bucket, and using the least privilege principle when defining IAM policies for Lambda.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here are the steps to use AWS Lambda security best practices using AWS CLI:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>Resource-based policies for Lambda function:  </li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>a. Create a resource-based policy for your Lambda function using the AWS CLI by running the following command: </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code><code>aws lambda create-policy --function-name &lt;function_name&gt; --policy &lt;policy_document&gt;</code></code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>b. Replace <code>&lt;function_name&gt;</code> with the name of your Lambda function and <code>&lt;policy_document&gt;</code> with the JSON document that defines the policy.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>   2. Resource-based policies for S3 bucket: a. Create a resource-based policy for your S3 bucket using the AWS CLI by running the following command: </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code><code>aws s3api put-bucket-policy --bucket &lt;bucket_name&gt; --policy &lt;policy_document&gt;</code></code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p> b. Replace <code>&lt;bucket_name&gt;</code> with the name of your S3 bucket and <code>&lt;policy_document&gt;</code> with the JSON document that defines the policy.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>    3. IAM policies for Lambda: </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>a. Create an IAM policy using the AWS CLI by running the following command: </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code><code>aws iam create-policy --policy-name &lt;policy_name&gt; --policy-document &lt;policy_document&gt;</code></code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>b. Replace <code>&lt;policy_name&gt;</code> with the name of your IAM policy and <code>&lt;policy_document&gt;</code> with the JSON document that defines the policy. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>c. Attach the IAM policy to the appropriate IAM role or user by running the following command: </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>aws iam attach-role-policy --role-name &lt;role_name&gt; --policy-arn &lt;policy_arn&gt; </code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>d. Replace <code>&lt;role_name&gt;</code> with the name of the IAM role and <code>&lt;policy_arn&gt;</code> with the ARN of the IAM policy.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Note: These are high-level steps and the actual steps might differ based on the complexity of your infrastructure and the specific use case. Additionally, you can also use AWS CloudFormation or AWS CDK to automate the creation and deployment of policies. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can read more about the latest steps <a href="https://sudoconsultants.com/security-and-compliance-aws-lambda-2/" target="_blank" rel="noreferrer noopener">here</a>. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>It's important to note that security and compliance considerations are ongoing and should be regularly reviewed, updated and audited. AWS provides a number of tools and best practices to help you secure your Lambda environment, but it's your responsibility to implement them correctly and keep them up to date.</p>
<!-- /wp:paragraph -->