The function takes the processed image from the previous section - the person with the green background removed - and composites various graphics elements into a final image. This final image is saved in the final bucket.
In the last section, you built a Lambda function manually through the console. However, if your function contains packages or libraries, it’s easier to use automation to help you bundle assets together for deployment.
In this part, you will use the AWS Serverless Application Model (SAM) to automate the package and deployment of the function.
The AWS SAM template file is a YAML or JSON configuration file. You use the template to declare all of the AWS resources that make up your serverless application.
Go to your browser tab with Cloud9 running. If you need to re-launch Cloud9, from the AWS Management Console, select Services then select Cloud9 under Developer Tools. Make sure your region is correct.
In the Cloud9 file explorer panel, navigate to and open theme-park-backend\3-photos\2-compositing\template.yaml to review its contents.

SAM will read this file and convert this YAML into infrastructure. Some of the important sections for today include:
Within the Resources section:
lambdaFunction directory).FinalBucketName parameter as an input.cd ~/environment/theme-park-backend/3-photos/2-compositing
$FINAL_BUCKET. SAM will be configured to use this S3 bucket name to set the environment variable within the Lambda function.aws s3 ls | grep finalbucket
sam build
sam package --output-template-file packaged.yaml --s3-bucket $s3_deploy_bucket
sam deploy --template-file packaged.yaml --stack-name theme-park-photos --capabilities CAPABILITY_IAM --parameter-overrides "FinalBucketName"=$FINAL_BUCKET
This will take a few minutes to deploy - wait for the confirmation message in the console before continuing.
Now you have created the Lambda function, you need to configure how it is invoked. This compositing function needs to execute when a new object is put into the processingbucket. In this section, you will create this trigger.
Go to the Lambda console - from the AWS Management Console, select Services then select Lambda under Compute. Make sure your region is correct.
Select the function with the name theme-park-photos-CompositeFunction-XXXXXXXXX.
Select + Add Trigger:
theme-park-backend-processingbucket.You will now test the function using a test image containing a photo of a person against a green background. You will copy this image into the uploadbucket, and see the final result in the finalbucket.
Go back to your browser tab with Cloud9 running. If you need to re-launch Cloud9, from the AWS Management Console, select Services then select Cloud9 under Developer Tools. Make sure your region is correct.
In the terminal enter the following command to change the directory:
cd ~/environment/theme-park-backend/3-photos/
aws s3 ls | grep uploadbucket
$UPLOAD_BUCKET which is set to this bucket name:aws s3 cp ./green-screen-test.png s3://$UPLOAD_BUCKET

theme-park-backend-finalbucket.
Check the green-screen-test.jpg object, then select Download.
Note: if you don’t see the file in the bucket, check that “Block Public Access” S3 setting is turned OFF for your AWS account. If it is ON, you will need to turn it off, repeat step 4 again and refresh the final S3 bucket to see if the file has appeared.
Save the file locally and open in an image viewer.
You will see photo of the person has had the green background removed, and is now composited with the theme park background and logo graphics.
Next, you will create the post-process Lambda function to store result in DynamoDB and notify the front-end.