Skip to content

Commit e1a9503

Browse files
committed
create a separate script for role creation
1 parent 362a18d commit e1a9503

File tree

3 files changed

+45
-48
lines changed

3 files changed

+45
-48
lines changed

Examples/_MyFirstFunction/clean.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/bin/sh
22

3-
alias aws="aws --profile seb"
4-
53
echo "This script deletes the Lambda function and the IAM role created in the previous step and deletes the project files."
64
read -p "Are you you sure you want to delete everything that was created? [y/n] " continue
75
if [[ ! $continue =~ ^[Yy]$ ]]; then

Examples/_MyFirstFunction/create_and_deploy_function.sh

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
# Stop the script execution if an error occurs
44
set -e -o pipefail
55

6-
alias aws="aws --profile seb"
7-
86
# 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
7+
which docker > /dev/null || (echo "Docker is not installed. Please install Docker and try again." && exit 1)
8+
9+
# check if aws cli is installed
10+
which aws > /dev/null || (echo "AWS CLI is not installed. Please install AWS CLI and try again." && exit 1)
11+
12+
# import code present in create_iam_role.sh
13+
source ./create_iam_role.sh
1414

1515
# check if user has an access key and secret access key
1616
echo "This script creates and deploys a Lambda function on your AWS Account.
@@ -86,45 +86,6 @@ echo "🚀 Deploy to AWS Lambda"
8686
echo "🔑 Retrieve your AWS Account ID"
8787
export AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
8888

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-
12889
# Check if the role already exists
12990
echo "🔍 Check if a Lambda execution IAM role already exists"
13091
aws iam get-role --role-name lambda_basic_execution > /dev/null 2>&1 || create_lambda_execution_role lambda_basic_execution
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#
2+
# Create an IAM role for the Lambda function
3+
#
4+
create_lambda_execution_role() {
5+
role_name=$1
6+
7+
# Allow the Lambda service to assume the IAM role
8+
cat <<EOF > trust-policy.json
9+
{
10+
"Version": "2012-10-17",
11+
"Statement": [
12+
{
13+
"Effect": "Allow",
14+
"Principal": {
15+
"Service": "lambda.amazonaws.com"
16+
},
17+
"Action": "sts:AssumeRole"
18+
}
19+
]
20+
}
21+
EOF
22+
23+
# Create the IAM role
24+
echo "🔐 Create the IAM role for the Lambda function"
25+
aws iam create-role \
26+
--role-name $role_name \
27+
--assume-role-policy-document file://trust-policy.json > /dev/null 2>&1
28+
29+
# Attach basic permissions to the role
30+
# The AWSLambdaBasicExecutionRole policy grants permissions to write logs to CloudWatch Logs
31+
echo "🔒 Attach basic permissions to the role"
32+
aws iam attach-role-policy \
33+
--role-name $role_name \
34+
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole > /dev/null 2>&1
35+
36+
echo "⏰ Waiting 10 secs for IAM role to propagate..."
37+
sleep 10
38+
}

0 commit comments

Comments
 (0)