-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdeploy-cloudformation.sh
More file actions
executable file
·79 lines (67 loc) · 2.67 KB
/
deploy-cloudformation.sh
File metadata and controls
executable file
·79 lines (67 loc) · 2.67 KB
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
#!/bin/bash
set -e
# Configuration
STACK_NAME="pandoc-lambda-stack"
TEMPLATE_FILE="template.yaml"
AWS_REGION=${AWS_REGION:-"us-east-1"}
# Check if AWS CLI is installed
if ! command -v aws &> /dev/null; then
echo "AWS CLI not found. Please install it first."
exit 1
fi
# Check if template file exists
if [ ! -f "${TEMPLATE_FILE}" ]; then
echo "Template file not found: ${TEMPLATE_FILE}"
exit 1
fi
# Check if stack exists and get its status
STACK_STATUS=$(aws cloudformation describe-stacks --stack-name ${STACK_NAME} --region ${AWS_REGION} --query "Stacks[0].StackStatus" --output text 2>/dev/null || echo "STACK_DOES_NOT_EXIST")
echo "Current stack status: ${STACK_STATUS}"
# Handle based on stack status
if [ "${STACK_STATUS}" == "STACK_DOES_NOT_EXIST" ]; then
# Create stack
echo "Creating CloudFormation stack ${STACK_NAME}..."
aws cloudformation create-stack \
--stack-name ${STACK_NAME} \
--template-body file://${TEMPLATE_FILE} \
--capabilities CAPABILITY_NAMED_IAM \
--region ${AWS_REGION}
echo "Waiting for stack creation to complete..."
aws cloudformation wait stack-create-complete \
--stack-name ${STACK_NAME} \
--region ${AWS_REGION}
elif [ "${STACK_STATUS}" == "ROLLBACK_COMPLETE" ]; then
# Delete stack if it's in ROLLBACK_COMPLETE state
echo "Stack is in ROLLBACK_COMPLETE state. Deleting the stack first..."
aws cloudformation delete-stack \
--stack-name ${STACK_NAME} \
--region ${AWS_REGION}
echo "Waiting for stack deletion to complete..."
aws cloudformation wait stack-delete-complete \
--stack-name ${STACK_NAME} \
--region ${AWS_REGION}
# Create new stack
echo "Creating new CloudFormation stack ${STACK_NAME}..."
aws cloudformation create-stack \
--stack-name ${STACK_NAME} \
--template-body file://${TEMPLATE_FILE} \
--capabilities CAPABILITY_NAMED_IAM \
--region ${AWS_REGION}
echo "Waiting for stack creation to complete..."
aws cloudformation wait stack-create-complete \
--stack-name ${STACK_NAME} \
--region ${AWS_REGION}
else
# Update stack
echo "Updating CloudFormation stack ${STACK_NAME}..."
aws cloudformation update-stack \
--stack-name ${STACK_NAME} \
--template-body file://${TEMPLATE_FILE} \
--capabilities CAPABILITY_NAMED_IAM \
--region ${AWS_REGION} || echo "No updates to be performed."
echo "Waiting for stack update to complete..."
aws cloudformation wait stack-update-complete \
--stack-name ${STACK_NAME} \
--region ${AWS_REGION}
fi
echo "CloudFormation deployment completed successfully!"