Skip to content

Instantly share code, notes, and snippets.

@ajazfarhad
Created November 5, 2020 10:04
Show Gist options
  • Save ajazfarhad/105c3b91108f9989cd8480766f271e5d to your computer and use it in GitHub Desktop.
Save ajazfarhad/105c3b91108f9989cd8480766f271e5d to your computer and use it in GitHub Desktop.
Secure file transfer on Fargate service
#!/bin/bash
CLUSTER="cluster-name"
SERVICE="service/service-name"
last_parameter=${@:$#} # last parameter
other_parameters=${*%${!#}} # all parameters except the last
helpFunction()
{
echo ""
echo "Usage: $0 /[source file path] /[desitnation path]"
exit 1
}
# Print helpFunction in case parameter is empty
[[ -z "$last_parameter" ]] || [[ -z "$other_parameters" ]] && echo "File Path parameters is empty" && helpFunction
if [ -z "${AWS_ACCESS_KEY_ID}" ]; then
echo "AWS_ACCESS_KEY_ID environment variable must be set"
exit 1
fi
if [ -z "${AWS_DEFAULT_REGION}" ]; then
echo "AWS_DEFAULT_REGION environment variable must be set"
exit 1
fi
if [ -z "${AWS_SECRET_ACCESS_KEY}" ]; then
echo "AWS_SECRET_ACCESS_KEY environment variable must be set"
exit 1
fi
task=""
task=$(aws ecs list-tasks --service=${SERVICE} --cluster=${CLUSTER} | jq -r '.taskArns[0]')
[[ -z "$task" ]] && echo "No running tasks found, are you sure the environment is up?" && exit 1
# Find the Elastic IP associated with a running service, then use that to find the
# public ip
interface_id=$(aws ecs describe-tasks --task=${task} --cluster=${CLUSTER} | jq -r '.tasks[] | select(.desiredStatus="RUNNING").attachments[] | select(.type == "ElasticNetworkInterface").details[] | select(.name == "networkInterfaceId").value')
public_ip=$(aws ec2 describe-network-interfaces --network-interface-ids ${interface_id} | jq -r '.NetworkInterfaces[0].Association.PublicIp')
# Copying file on server
scp $other_parameters app@$public_ip:$last_parameter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment