Clean up resources

Clean up resources

Delete the lab resources in the order below to avoid ongoing charges. The order matters: a Lambda@Edge function cannot be deleted while it is still associated with a CloudFront distribution, and a distribution must be disabled before it can be deleted.

Deleted resources cannot be recovered. Check the name of each resource before you delete it so you only remove resources created in this lab.

Step 1 - Remove the Lambda@Edge association

  1. Open the CloudFront console and select Distributions, then select the distribution you created in this lab.
  2. Open the Behaviors tab, select the /serverless behavior, and choose Edit.
  3. Under Function associations, set Viewer request (or the event type you used) to No association.
  4. Choose Save changes. Wait until the distribution Last modified column shows a date instead of Deploying.

Step 2 - Disable and delete the CloudFront distribution

  1. In Distributions, select your distribution and choose Disable, then confirm with Disable.
  2. Wait until the status changes from Deploying to a date in Last modified. This can take several minutes.
  3. Select the distribution again and choose Delete, then confirm with Delete.

The Delete button stays unavailable until the distribution is fully disabled.

Step 3 - Delete the Lambda@Edge function

  1. Switch the Region to US East (N. Virginia) us-east-1.
  2. Open the Lambda console, select Functions, and select LambdaEdgeImmersionDayLabFunction.
  3. Choose Actions > Delete, type confirm in the box, and choose Delete.

After you remove the association, CloudFront needs time to delete the function replicas at the edge locations. If you see the error “Lambda was unable to delete … because it is a replicated function”, wait a few hours and try again. The replicas do not incur charges while you wait.

  1. Open IAM > Roles, find the execution role that was created for the function (its name starts with LambdaEdgeImmersionDayLabFunction-role), and choose Delete. Type the role name to confirm.
  2. Optional: open CloudWatch > Log groups in the Regions where you tested the function and delete the log groups named /aws/lambda/us-east-1.LambdaEdgeImmersionDayLabFunction.

Step 4 - Delete the S3 buckets

Repeat these steps for the primary bucket (cloudfront-lab-<account-id>-<region>-an) and the secondary bucket (s3bucket-secondary…) from the origin group section.

  1. Open the S3 console and select the bucket.
  2. Choose Empty, type permanently delete, and choose Empty.
  3. Go back to the bucket list, select the bucket, choose Delete, enter the bucket name, and choose Delete bucket.

A bucket must be empty before it can be deleted. You can also empty and delete a bucket with one AWS CLI command:

aws s3 rb s3://BUCKET-NAME --force

Replace BUCKET-NAME with your bucket name.

Step 5 - Terminate the EC2 instance

  1. Open the EC2 console and select Instances.
  2. Select the instance you created for this lab.
  3. Choose Instance state > Terminate (delete) instance, then confirm with Terminate (delete).
  4. Wait until Instance state shows Terminated.
  5. In Key Pairs, select cloudfront-lab and choose Actions > Delete. Type Delete to confirm.
  6. In Security Groups, select cloudfront-sg and choose Actions > Delete security groups.

The security group can only be deleted after the instance is terminated.

Step 6 - Delete the VPC

  1. Open the VPC console and select Your VPCs.
  2. Select cloudfront-lab-vpc and choose Actions > Delete VPC.
  3. Type delete to confirm and choose Delete. This also deletes the subnets, route table, and Internet Gateway created with the VPC.