Javi

AWS: Create instance

Oct 31st, 2024 (edited)
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. aws ec2 describe-vpcs --filters "Name=isDefault,Values=true" --query "Vpcs[0].VpcId" --output text
  2.  
  3. DEFAULTVPCID=$(aws ec2 describe-vpcs --filters "Name=isDefault,Values=true" --query "Vpcs[0].VpcId" --output text)
  4.  
  5. echo Your VPC is $DEFAULTVPCID.
  6.  
  7.  
  8.  
  9.  
  10. aws ec2 describe-subnets --filters "Name=vpc-id,Values=$DEFAULTVPCID" --query "Subnets[0].SubnetId" --output text
  11.  
  12. SUBNETID=$(aws ec2 describe-subnets --filters "Name=vpc-id,Values=$DEFAULTVPCID" --query "Subnets[0].SubnetId" --output text)
  13.  
  14. echo Your subnet is $SUBNETID.
  15.  
  16.  
  17.  
  18.  
  19. SG=$(aws ec2 create-security-group \
  20. --group-name AppSG\
  21. --description "The security group of the application." \
  22. --vpc-id $DEFAULTVPCID\
  23. --query 'GroupId' \
  24. --output text)
  25. echo The security group is $SG.
  26.  
  27. aws ec2 authorize-security-group-ingress \
  28. --group-id $SG \
  29. --protocol tcp \
  30. --port 8080 \
  31. --cidr 0.0.0.0/0
  32.  
  33.  
  34.  
  35. AMI=$(aws ec2 describe-images \
  36. --owners 099720109477 \
  37. --filters 'Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-*-22.04-amd64-server-*' 'Name=state,Values=available' \
  38. --query 'sort_by(Images, &CreationDate)[-1].[ImageId]' \
  39. --output text)
  40. echo The AMI is going to be $AMI.
  41.  
  42. cat << EOF > pokemon.sh
  43. #!/bin/sh
  44.  
  45. sudo apt update
  46. sudo apt install openjdk-17-jre-headless -y
  47. wget https://github.com/ciberado/pokemon-java/releases/download/v2.0.0/pokemon-2.0.0.jar
  48. java -jar pokemon-2.0.0.jar
  49. EOF
  50.  
  51.  
  52. cat pokemon.sh
  53.  
  54.  
  55.  
  56.  
  57. aws ec2 run-instances \
  58. --subnet-id $SUBNETID\
  59. --image-id $AMI \
  60. --security-group-ids $SG \
  61. --instance-type t3.nano \
  62. --block-device-mapping DeviceName=/dev/sda1,Ebs={VolumeSize=8} \
  63. --user-data file://pokemon.sh \
  64. --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=PokemonServer},{Key=App,Value=Pokemon}]"
  65.  
  66.  
  67.  
  68.  
  69. IP=$(aws ec2 describe-instances \
  70. --filters "Name=tag:Name,Values=PokemonServer" \
  71. --query 'Reservations[*].Instances[*].PublicIpAddress' \
  72. --output text)
  73. echo Your application is at http://$IP:8080
  74.  
  75. echo And your Pokémon is $(curl -s http://$IP:8080/ | jq .name -r).
  76.  
  77.  
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment