-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildspec.yml
85 lines (85 loc) · 6.59 KB
/
buildspec.yml
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
version: 0.2
phases:
build:
commands:
- |
if [ "$ACTION" = "destroy" ]; then
USER_NODE="{}"
else
USER_NODE="{\"${NODE_ID}\"=\"${NODE_PORT}\"}"
fi
echo "USER_NODE: ${USER_NODE}"
- export TFSTATE_PATH="s3://rln-${ENV}-terraform-backend/terraform_backend/${USER_ID}/${NODE_ID}.tfstate"
- export DEFAULT_LB_ARN="arn:aws:elasticloadbalancing:us-east-2:339712759892:loadbalancer/net/vpc-link-nlb-public/1c83ff42632a54a8"
- export DEFAULT_LB_DNS="vpc-link-nlb-public-1c83ff42632a54a8.elb.us-east-2.amazonaws.com"
- export DEFAULT_LB_ID="rf56qp"
- |
if aws s3 ls "$TFSTATE_PATH" > /dev/null 2>&1; then
echo "tfstate file found. Downloading and parsing..."
aws s3 cp "$TFSTATE_PATH" ./node.tfstate
LB_ARN=$(cat node.tfstate | jq -r '.outputs.network_load_balancer_arn | select(. != null) | .value')
LB_DNS=$(cat node.tfstate | jq -r '.outputs.network_load_balancer_dns | select(. != null) | .value')
VPC_LINK_ID=$(cat node.tfstate | jq -r '.outputs.api_gateway_vpclink_id | select(. != null) | .value')
echo "LB_ARN: $LB_ARN"
echo "LB_DNS: $LB_DNS"
echo "VPC_LINK_ID: $VPC_LINK_ID"
if [ "$LB_ARN" = "arn:aws:elasticloadbalancing:us-east-2:339712759892:loadbalancer/net/vpc-link-nlb-public/1c83ff42632a54a8" ]; then
export CHOSEN_LB_ARN="arn:aws:elasticloadbalancing:us-east-2:339712759892:loadbalancer/net/vpc-link-nlb-public-3/c5fe8631053259f3"
export CHOSEN_LB_DNS="vpc-link-nlb-public-3-c5fe8631053259f3.elb.us-east-2.amazonaws.com"
export CHOSEN_LB_ID="i6hhpe"
else
export CHOSEN_LB_ARN=${LB_ARN:-$DEFAULT_LB_ARN}
export CHOSEN_LB_DNS=${LB_DNS:-$DEFAULT_LB_DNS}
export CHOSEN_LB_ID=${VPC_LINK_ID:-$DEFAULT_LB_ID}
fi
rm -f node.tfstate
else
echo "tfstate file not found. Checking NLB listener counts..."
MAX_LISTENERS=48
export LB1_LISTENERS=$(aws elbv2 describe-listeners --load-balancer-arn "$DEFAULT_LB_ARN" --query "Listeners | length(@)" --output text || echo 0)
export LB2_LISTENERS=$(aws elbv2 describe-listeners --load-balancer-arn "arn:aws:elasticloadbalancing:us-east-2:339712759892:loadbalancer/net/vpc-link-nlb-public-2/c83eb6aa70e6aec9" --query "Listeners | length(@)" --output text || echo 0)
export LB3_LISTENERS=$(aws elbv2 describe-listeners --load-balancer-arn "arn:aws:elasticloadbalancing:us-east-2:339712759892:loadbalancer/net/vpc-link-nlb-public-3/c5fe8631053259f3" --query "Listeners | length(@)" --output text || echo 0)
export LB4_LISTENERS=$(aws elbv2 describe-listeners --load-balancer-arn "arn:aws:elasticloadbalancing:us-east-2:339712759892:loadbalancer/net/vpc-link-nlb-public-4/99b41a4daf28069d" --query "Listeners | length(@)" --output text || echo 0)
if [ "$LB1_LISTENERS" -le "$MAX_LISTENERS" ]; then
export CHOSEN_LB_ARN="$DEFAULT_LB_ARN"
export CHOSEN_LB_DNS="$DEFAULT_LB_DNS"
export CHOSEN_LB_ID="$DEFAULT_LB_ID"
elif [ "$LB2_LISTENERS" -le "$MAX_LISTENERS" ]; then
export CHOSEN_LB_ARN="arn:aws:elasticloadbalancing:us-east-2:339712759892:loadbalancer/net/vpc-link-nlb-public-2/c83eb6aa70e6aec9"
export CHOSEN_LB_DNS="vpc-link-nlb-public-2-c83eb6aa70e6aec9.elb.us-east-2.amazonaws.com"
export CHOSEN_LB_ID="z4wm6y"
elif [ "$LB3_LISTENERS" -le "$MAX_LISTENERS" ]; then
export CHOSEN_LB_ARN="arn:aws:elasticloadbalancing:us-east-2:339712759892:loadbalancer/net/vpc-link-nlb-public-3/c5fe8631053259f3"
export CHOSEN_LB_DNS="vpc-link-nlb-public-3-c5fe8631053259f3.elb.us-east-2.amazonaws.com"
export CHOSEN_LB_ID="i6hhpe"
else
export CHOSEN_LB_ARN="arn:aws:elasticloadbalancing:us-east-2:339712759892:loadbalancer/net/vpc-link-nlb-public-4/99b41a4daf28069d"
export CHOSEN_LB_DNS="vpc-link-nlb-public-4-99b41a4daf28069d.elb.us-east-2.amazonaws.com"
export CHOSEN_LB_ID="o98iws"
fi
fi
- echo $LB1_LISTENERS
- echo $LB2_LISTENERS
- echo $LB3_LISTENERS
- echo $CHOSEN_LB_ARN
- export TF_VAR_network_load_balancer_arn=$CHOSEN_LB_ARN
- export TF_VAR_network_load_balancer_dns=$CHOSEN_LB_DNS
- export TF_VAR_api_gateway_vpclink_id=$CHOSEN_LB_ID
- |
VOLUME_ID=$(aws ec2 describe-volumes --filters Name=tag:Name,Values="rln-ebs-${USER_ID}-${NODE_ID}" --query "Volumes[0].VolumeId" --output text)
if aws ec2 describe-volumes --volume-ids $VOLUME_ID | grep -q "InstanceId"; then
echo "Detaching volume $VOLUME_ID..."
aws ec2 detach-volume --volume-id $VOLUME_ID
aws ec2 wait volume-available --volume-ids $VOLUME_ID
fi
- cd shared
- terraform init -backend-config="bucket=rln-${ENV}-terraform-backend" -backend-config="region=us-east-2" -backend-config="key=terraform_backend/${USER_ID}-shared.tfstate"
- terraform apply --auto-approve -var="user_id=${USER_ID}" -var="region=us-east-2"
- cd ../main
- terraform init -backend-config="bucket=rln-${ENV}-terraform-backend" -backend-config="region=us-east-2" -backend-config="key=terraform_backend/${USER_ID}/${NODE_ID}.tfstate"
- terraform validate
- terraform plan -var="user_id=${USER_ID}" -var="user_node_ids=${USER_NODE}" -var="btc_network=${BTC_NETWORK}" -var="btc_rpc=${BTC_RPC}" -var="env=${ENV}" -var="cognito_authorizer_id=${COGNITO_AUTH_ID}" -var="token_authorizer_id=${TOKEN_AUTHORIZER_ID}" -var=docker_image_tag=${DOCKER_IMAGE_TAG} -var=ecr_healthcheck_repository_url=${DOCKER_HEALTHCHECK_IMAGE_REPO} -var=docker_healthcheck_image_tag=${DOCKER_HEALTHCHECK_IMAGE_TAG} -var=network_load_balancer_arn=${CHOSEN_LB_ARN} -var=network_load_balancer_dns=${CHOSEN_LB_DNS} -var=api_gateway_vpclink_id=${CHOSEN_LB_ID}
- terraform apply --auto-approve -var="user_id=${USER_ID}" -var="user_node_ids=${USER_NODE}" -var="btc_network=${BTC_NETWORK}" -var="btc_rpc=${BTC_RPC}" -var="env=${ENV}" -var="cognito_authorizer_id=${COGNITO_AUTH_ID}" -var="token_authorizer_id=${TOKEN_AUTHORIZER_ID}" -var=docker_image_tag=${DOCKER_IMAGE_TAG} -var="region=us-east-2" -var=ecr_healthcheck_repository_url=${DOCKER_HEALTHCHECK_IMAGE_REPO} -var=docker_healthcheck_image_tag=${DOCKER_HEALTHCHECK_IMAGE_TAG} -var=network_load_balancer_arn=${CHOSEN_LB_ARN} -var=network_load_balancer_dns=${CHOSEN_LB_DNS} -var=api_gateway_vpclink_id=${CHOSEN_LB_ID}
- cd ../deployment
- terraform init
- terraform apply --replace=aws_api_gateway_deployment.deployment --auto-approve || true