Important Note: You will create AWS resources during the exercise which will incur cost in your AWS account. It is recommended to clean-up the resources as soon as you finish the exercise to minimize the cost.

Using Temp Directory Storage in Lambda

The Lambda execution environment provides a file system for the code to use at /tmp. The file system is local to the Lambda function and can be used for read-write operations. This space has a fixed size of 512 MB. The same Lambda execution environment may be reused by multiple Lambda invocations to optimize performance.

Step1: Pre-Requisite


You need to have an AWS account with administrative access to complete the exercise. If you don’t have an AWS account, kindly use the link to create free trial account for AWS.

Step2: Create IAM Role


You start with creation of the IAM role which AWS Lambda function uses for the authorization to call other AWS Services.

  1. Login to the AWS Console. Select Paris as the region.

  2. Goto the IAM Management console and click on the Roles menu in the left and then click on the Create role button.

    Lambda

  3. On the next screen, select Lambda as the service and click on the Next: Permissions button.

    Lambda

  4. On the next screen, select PowerUserAccess as the policy and click on the Next: Tags button.

    Lambda

  5. On the next screen, click on the Next: Review button.

  6. On the next screen, type in dojolambdarole for the Role name and click on the Create role button.

    Lambda

  7. The role is created in no time. The next step is to create the S3 bucket and upload a sample data file.

Step3: Create S3 Bucket


You create an Amazon S3 bucket and upload a sample file to it. The Lambda function will later download this sample file from the S3 bucket to the temporary directory storage.

  1. Download the sample data file from the link. The data in the file looks like the following -

    Lambda

  2. Goto AWS S3 Management Console. Create an S3 bucket with the name dojo-data-bucket. If this bucket name already exists; create a bucket with the name which is available. In this bucket, upload the sample data file you downloaded in the previous step.

    Lambda

  3. The S3 bucket and the data is ready. Let’s configure Lambda function in the next step.

Step4: Create Lambda Function


You create a Lambda function which is configured to read-write data with temporary directory storage.

  1. Goto Lambda Management console and click on the Create function button.

    Lambda

  2. On the next screen, select Author from scratch as the option. Type in dojolambda as the Function name. Select Python 3.8 as the Runtime. Under Permissions, select Use an existing role as the option and then select dojolambdarole (you created in the earlier steps) as the role. Finally, click on the Create function button.

    Lambda

  3. The Lambda function is now ready. Goto the Function code setting for the lambda function and replace the code with the following code below and then click on the Deploy button.

    Lambda

import json
import boto3

def lambda_handler(event, context):
    
    s3_client = boto3.client('s3')
    
    bucket = event["bucket"]
    key = event["key"]
    datainput = event["input"]
    
    filepath = "/tmp/" +  key
    
    s3_client.download_file(bucket, key, filepath)
    
    f = open(filepath, "a")
    f.write("\n" + datainput)
    f.close()
    
    s3_client.upload_file(filepath, bucket, key)
    
    return {
        'statusCode': 200,
        'body': json.dumps("File updated successfully.")
    }
  1. The function code is updated. In the code above, you create boto3 client for S3. You first download the sample data file from the S3 bucket to the temporary directory storage. You then modify the file at the temporary directory storage and upload the updated file back to the S3 bucket. Finally, the function returns a success message. The bucket name, object key (filename) and new data input for the file is read from the input parameter (json) provided to the Lambda function at the run time.

  2. The lambda function code and configuration is ready. You will run it in the next step.

Step5: Run Lambda Function


Time to run the lambda function.

  1. In Lambda Management console, for the function created in the previous step, click on the Test button.

    Lambda

  2. On the next screen, type in dojotest for the Event name. Copy-paste the below json document as input. The json provides input for the key (filename), bucket and new data input. If you created bucket with a different name, provide that bucket name here. Finally, click on the Create button.

    Lambda

    {
      "key": "dojodata.txt",
      "bucket": "dojo-data-bucket",
      "input" : "new-test-data"
    }
    

    `

  3. The test is created. Keeping dojotest selected, click on the Test button again.

    Lambda

  4. The function runs with the input provided in the test configuration and returns the success message.

    Lambda

  5. You can check the sample data file in S3. It would have been updated with the new data provided through the Lambda input.

    Lambda

  6. This concludes the exercise. Please follow the next step to clean-up the resources so that you don’t incur any cost post the exercise.

Step6: Clean up


Delete dojolambda function in the AWS Lambda console.

Delete the dojo-data-bucket bucket in the S3 Management Console.

Delete dojolambdarole IAM role from the IAM Management console.

Thanks and hope you enjoyed the exercise.


Back to the Exercises