whs2k
whs2k

Reputation: 771

How To Connect to an already created Chalice Project and AWS Lambda Environment?

Beginner Chalice question here!

I would like to connect to and modify a chalice project created by a team member that includes multiple lambda functions. I have already setup my AWS environment to connect to the same account, have git cloned the code, and can see the Lambda functions that were already created. My question is: How do I setup my Chalice environment such that when I do a chalice deploy the existing lambda functions are updated and I'm not creating a bunch of new ones (and I AM roles etc) in our AWS Account?

In the Chalice CLI source code the only project initiation command listed is chalice new-project; that is, there is no chalice connect-to-existing-project --name XXX. If I do the following will my lambda functions be updated instead of creating new ones: (1) ensure i'm connected to the same AWS account (2) keep the project folders the same names and (3) keep my chalice routes in the code the same name? Wanted to check before I start spamming resources; any details appreciated!

Upvotes: 1

Views: 505

Answers (1)

ARKhoshghalb
ARKhoshghalb

Reputation: 95

This is an old question but for anyone having this problem now, you need to sync the files in .chalice/deployed folder with the team so everyone would use the same resources. The files there basically keep track of what's been deployed so far and what are their ids.

There are a few ways you could do this. Either store it in git (not very nice but still works for a small team) or, write a script that syncs the files through an S3 bucket and instead of calling chalice deploy run another command than syncs the file with your bucket first, then runs chalice deploy, then syncs the files again.

If your team is even larger and you want to avoid multiple people deploying at the same time, use a Dynamo table to lock the files when they are being used. Basically, do what Terraform does with saving a "remote state".

I'll put one version of such script that syncs files between S3 and local. I use this both when I deploy from my local computer or through Github Actions:

#!/bin/bash

# Check if the first argument is provided
if [ $# -eq 0 ]; then
    echo "Usage: $0 <upload|download>"
    exit 1
fi

# Get the action (upload or download) from the first argument
action=$1
# Get the AWS profile from the second argument (optional)
aws_profile=$2

bucket_name="... your bucket name ..."

# Define the local and S3 paths
local_path="src/.chalice/deployed"
s3_destination="s3://$bucket_name/<service name>/deployed"

# Perform the specified action
case $action in
    "upload")
        # Upload the folder and its content to S3
        # If aws_profile is provided, pass the argument
        if [ -n "$aws_profile" ]; then
            aws s3 sync "$local_path" "$s3_destination" --profile "$aws_profile"
        else
            aws s3 sync "$local_path" "$s3_destination"
        fi
        echo "Upload complete."
        ;;
    "download")
        # Download the folder and its content from S3
        # If aws_profile is provided, pass the argument
        if [ -n "$aws_profile" ]; then
            aws s3 sync "$s3_destination" "$local_path" --profile "$aws_profile"
        else
            aws s3 sync "$s3_destination" "$local_path"
        fi
        echo "Download complete."
        ;;
    *)
        echo "Invalid action. Use 'upload' or 'download'."
        exit 1
        ;;
esac

Then in my Makefile I have:

# Usage: make deploy stage=dev aws_profile=your_profile
.PHONY: deploy
deploy:
    @./src/scripts/sync_deploy_files.sh download $(aws_profile)
    @[ -e "src/.chalice/deployed/$(stage).json" ] || { echo "Deploy file not found! Sync must have failed."; exit 1; }
#   Remove all other files
    @cd src/.chalice/deployed && find . ! -name '$(stage).json' -type f -exec rm -f {} +
    @if [ -n "$(aws_profile)" ]; then \
        cd src && chalice deploy --stage $(stage) --profile $(aws_profile);\
    else \
        cd src && chalice deploy --stage $(stage);\
    fi
    @./src/scripts/sync_deploy_files.sh upload $(aws_profile)

Upvotes: 0

Related Questions