-
Notifications
You must be signed in to change notification settings - Fork 4
/
provision_resources.sh
executable file
·136 lines (107 loc) · 4.08 KB
/
provision_resources.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
echo "Let's set up your resources"
sleep 1
rm -rf env_vars.sh
# Read AWS credentials
read -p "Enter your AWS Access Key ID: " AWS_ACCESS_KEY_ID
read -sp "Enter your AWS Secret Access Key: " AWS_SECRET_ACCESS_KEY
echo # For newline after entering secret key
echo "Warning: You need to give proper permission to this IAM user."
# Export AWS credentials as environment variables
export AWS_ACCESS_KEY_ID="$AWS_ACCESS_KEY_ID"
export AWS_SECRET_ACCESS_KEY="$AWS_SECRET_ACCESS_KEY"
# Ask for the AWS region
read -p "In which AWS region do you want to setup resources? (e.g., us-east-1): " AWS_REGION
# Change directories and update region in Pulumi configurations
cd pulumi
for dir in setup-s3-temp setup-s3-permanent setup-lambda setup-ecr setup-ecs-task-definition setup-ecs-cluster setup-ec2; do
cd $dir
pulumi config set aws:region "$AWS_REGION"
cd ..
done
# Get temporary and permanent bucket names
read -p "Enter the name for the temporary S3 bucket: " TEMP_BUCKET_NAME
read -p "Enter the name for the permanent S3 bucket: " PERMANENT_BUCKET_NAME
# Export bucket names as environment variables
export TEMP_BUCKET_NAME="$TEMP_BUCKET_NAME"
export PERMANENT_BUCKET_NAME="$PERMANENT_BUCKET_NAME"
# Deploy Pulumi stacks for S3 and Lambda
cd setup-s3-temp
pulumi refresh --yes # Sync the state before deploying
pulumi up --yes
cd ..
cd setup-s3-permanent
pulumi refresh --yes # Sync the state before deploying
pulumi up --yes
cd ..
cd setup-lambda
pulumi refresh --yes # Sync the state before deploying
pulumi up --yes
cd ..
# Ask for the key pair name for EC2 instance
read -p "Enter the name of the EC2 key pair: " KEY_PAIR_NAME
export KEY_PAIR_NAME="$KEY_PAIR_NAME"
cd setup-ec2
pulumi refresh --yes # Sync the state before deploying
pulumi up --yes
cd ..
# Ask for and validate ECR repository name
while true; do
read -p "Enter a valid name for the ECR repository (lowercase, use - or _ if needed): " ECR_REPOSITORY_NAME
if [[ $ECR_REPOSITORY_NAME =~ ^[a-z0-9]+([._-][a-z0-9]+)*$ ]]; then
break
else
echo "Invalid repository name. Please ensure it is lowercase and follows AWS ECR naming rules."
fi
done
# Export the repository name as an environment variable
export ECR_REPOSITORY_NAME="$ECR_REPOSITORY_NAME"
# Deploy Pulumi stack for ECR
cd setup-ecr
pulumi refresh --yes # Sync the state before deploying
pulumi up --yes
cd ..
# Wait for 2 seconds
sleep 2
echo "You need to push a Docker image to this ECR repository in order to move further."
# Check if the user has pushed the Docker image
while true; do
read -p "Have you pushed the Docker image to the ECR repository? (yes/no): " PUSHED_IMAGE
if [[ $PUSHED_IMAGE == "yes" ]]; then
break
elif [[ $PUSHED_IMAGE == "no" ]]; then
echo "Please push the Docker image and try again."
exit 1
else
echo "Invalid response. Please enter 'yes' or 'no'."
fi
done
# Get the image URI from the user
read -p "Enter the Docker image URI (you can find it in the ECR dashboard): " IMAGE_URI
# Export the image URI as an environment variable
export IMAGE_URI="$IMAGE_URI"
# Deploy Pulumi stacks for ECS task definition and cluster
cd setup-ecs-task-definition
pulumi refresh --yes # Sync the state before deploying
pulumi up --yes
cd ..
cd setup-ecs-cluster
pulumi refresh --yes # Sync the state before deploying
pulumi up --yes
cd ../..
# Save the environment variables to a file
echo "Saving environment variables to env_vars.sh..."
{
echo "export AWS_ACCESS_KEY_ID=\"$AWS_ACCESS_KEY_ID\""
echo "export AWS_SECRET_ACCESS_KEY=\"$AWS_SECRET_ACCESS_KEY\""
echo "export AWS_REGION=\"${AWS_REGION:-us-east-1}\""
echo "export TEMP_BUCKET_NAME=\"${TEMP_BUCKET_NAME:-my-temp-bucket}\""
echo "export PERMANENT_BUCKET_NAME=\"${PERMANENT_BUCKET_NAME:-my-permanent-bucket}\""
echo "export ECR_REPOSITORY_NAME=\"${ECR_REPOSITORY_NAME:-my-ecr-repo}\""
echo "export IMAGE_URI=\"${IMAGE_URI:-your-image-uri}\""
echo "export KEY_PAIR_NAME=\"${KEY_PAIR_NAME:-your-}\""
} > env_vars.sh || { echo "Failed to write to env_vars.sh"; }
# Source the env_vars.shs
source env_vars.sh
# Success message
echo "All your resources are setup successfully."