|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Stop the script execution if an error occurs |
| 4 | +set -e -o pipefail |
| 5 | + |
| 6 | +alias aws="aws --profile seb" |
| 7 | + |
| 8 | +# check if docker is installed |
| 9 | +which docker > /dev/null |
| 10 | +if [[ $? != 0 ]]; then |
| 11 | + echo "Docker is not installed. Please install Docker and try again." |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | + |
| 15 | +# check if user has an access key and secret access key |
| 16 | +echo "This script creates and deploys a Lambda function on your AWS Account. |
| 17 | +
|
| 18 | +You must have an AWS account and know an AWS access key, secret access key, and an optional session token. |
| 19 | +These values are read from '~/.aws/credentials'. |
| 20 | +" |
| 21 | + |
| 22 | +read -p "Are you ready to create your first Lambda function in Swift? [y/n] " continue |
| 23 | +if [[ ! $continue =~ ^[Yy]$ ]]; then |
| 24 | + echo "OK, try again later when you feel ready" |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | + |
| 28 | +echo "⚡️ Create your Swift Lambda project" |
| 29 | +swift package init --type executable --name MyLambda > /dev/null |
| 30 | + |
| 31 | +echo "📦 Add the AWS Lambda Swift runtime to your project" |
| 32 | +# The following commands are commented out until the `lambad-init` plugin will be release |
| 33 | +# swift package add-dependency https://github.com/swift-server/swift-aws-lambda-runtime.git --branch main |
| 34 | +# swift package add-dependency https://github.com/swift-server/swift-aws-lambda-events.git --branch main |
| 35 | +# swift package add-target-dependency AWSLambdaRuntime MyLambda --package swift-aws-lambda-runtime |
| 36 | +# swift package add-target-dependency AWSLambdaEvents MyLambda --package swift-aws-lambda-events |
| 37 | +cat <<EOF > Package.swift |
| 38 | +// swift-tools-version:6.0 |
| 39 | +
|
| 40 | +import PackageDescription |
| 41 | +
|
| 42 | +let package = Package( |
| 43 | + name: "swift-aws-lambda-runtime-example", |
| 44 | + platforms: [.macOS(.v15)], |
| 45 | + products: [ |
| 46 | + .executable(name: "MyLambda", targets: ["MyLambda"]) |
| 47 | + ], |
| 48 | + dependencies: [ |
| 49 | + .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main") |
| 50 | + ], |
| 51 | + targets: [ |
| 52 | + .executableTarget( |
| 53 | + name: "MyLambda", |
| 54 | + dependencies: [ |
| 55 | + .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime") |
| 56 | + ], |
| 57 | + path: "." |
| 58 | + ) |
| 59 | + ] |
| 60 | +) |
| 61 | +EOF |
| 62 | + |
| 63 | +echo "📝 Write the Swift code" |
| 64 | +# The following command is commented out until the `lambad-init` plugin will be release |
| 65 | +# swift package lambda-init --allow-writing-to-package-directory |
| 66 | +cat <<EOF > Sources/main.swift |
| 67 | +import AWSLambdaRuntime |
| 68 | +
|
| 69 | +let runtime = LambdaRuntime { |
| 70 | + (event: String, context: LambdaContext) in |
| 71 | + "Hello \(event)" |
| 72 | +} |
| 73 | +
|
| 74 | +try await runtime.run() |
| 75 | +EOF |
| 76 | + |
| 77 | +echo "📦 Compile and package the function for deployment (this might take a while)" |
| 78 | +swift package archive --allow-network-connections docker > /dev/null 2>&1 |
| 79 | + |
| 80 | +# |
| 81 | +# Now the function is ready to be deployed to AWS Lambda |
| 82 | +# |
| 83 | +echo "🚀 Deploy to AWS Lambda" |
| 84 | + |
| 85 | +# retrieve your AWS Account ID |
| 86 | +echo "🔑 Retrieve your AWS Account ID" |
| 87 | +export AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text) |
| 88 | + |
| 89 | +# |
| 90 | +# Create an IAM role for the Lambda function |
| 91 | +# |
| 92 | +create_lambda_execution_role() { |
| 93 | + role_name=$1 |
| 94 | + |
| 95 | + # Allow the Lambda service to assume the IAM role |
| 96 | + cat <<EOF > trust-policy.json |
| 97 | +{ |
| 98 | + "Version": "2012-10-17", |
| 99 | + "Statement": [ |
| 100 | + { |
| 101 | + "Effect": "Allow", |
| 102 | + "Principal": { |
| 103 | + "Service": "lambda.amazonaws.com" |
| 104 | + }, |
| 105 | + "Action": "sts:AssumeRole" |
| 106 | + } |
| 107 | + ] |
| 108 | +} |
| 109 | +EOF |
| 110 | + |
| 111 | + # Create the IAM role |
| 112 | + echo "🔐 Create the IAM role for the Lambda function" |
| 113 | + aws iam create-role \ |
| 114 | + --role-name $role_name \ |
| 115 | + --assume-role-policy-document file://trust-policy.json > /dev/null 2>&1 |
| 116 | + |
| 117 | + # Attach basic permissions to the role |
| 118 | + # The AWSLambdaBasicExecutionRole policy grants permissions to write logs to CloudWatch Logs |
| 119 | + echo "🔒 Attach basic permissions to the role" |
| 120 | + aws iam attach-role-policy \ |
| 121 | + --role-name $role_name \ |
| 122 | + --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole > /dev/null 2>&1 |
| 123 | + |
| 124 | + echo "⏰ Waiting 10 secs for IAM role to propagate..." |
| 125 | + sleep 10 |
| 126 | +} |
| 127 | + |
| 128 | +# Check if the role already exists |
| 129 | +echo "🔍 Check if a Lambda execution IAM role already exists" |
| 130 | +aws iam get-role --role-name lambda_basic_execution > /dev/null 2>&1 || create_lambda_execution_role lambda_basic_execution |
| 131 | + |
| 132 | +# Create the Lambda function |
| 133 | +echo "🚀 Create the Lambda function" |
| 134 | +aws lambda create-function \ |
| 135 | +--function-name MyLambda \ |
| 136 | +--zip-file fileb://.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip \ |
| 137 | +--runtime provided.al2 \ |
| 138 | +--handler provided \ |
| 139 | +--architectures $(uname -m) \ |
| 140 | +--role arn:aws:iam::${AWS_ACCOUNT_ID}:role/lambda_basic_execution > /dev/null 2>&1 |
| 141 | + |
| 142 | +echo "⏰ Waiting 10 secs for the Lambda function to be ready..." |
| 143 | +sleep 10 |
| 144 | + |
| 145 | +# Invoke the Lambda function |
| 146 | +echo "🔗 Invoke the Lambda function" |
| 147 | +aws lambda invoke \ |
| 148 | +--function-name MyLambda \ |
| 149 | +--cli-binary-format raw-in-base64-out \ |
| 150 | +--payload '"Lambda Swift"' \ |
| 151 | +output.txt > /dev/null 2>&1 |
| 152 | + |
| 153 | +echo "👀 Your Lambda function returned:" |
| 154 | +cat output.txt && rm output.txt |
| 155 | + |
| 156 | +echo "" |
| 157 | +echo "🎉 Done! Your first Lambda function in Swift is now deployed on AWS Lambda. 🚀" |
0 commit comments