Accessing AWS EC2 instances through ELB

前端 未结 2 1164
萌比男神i
萌比男神i 2021-02-13 22:15

I\'m trying to set up two instances under an elastic load balancer, but cannot figure out how I\'m supposed to access the instances through the load balancer.

I\'ve set

2条回答
  •  一个人的身影
    2021-02-13 22:28

    I went ahead and created a script that will reproduce the same exact steps that i'm using. This assumes you're using linux as an operating system and that the AWS CLI tools are already installed. If you don't have this setup already I recommend starting a new Amazon Linux micro instance and running the script from there since they have everything already installed.

    1. Download the X.509 certificate files from amazon https://aws-portal.amazon.com/gp/aws/securityCredentials

    2. Copy the certificate files to the machine where you will run the commands

    3. Save two variables that are required in the script

      aws_account=
      keypair=""
      
    4. Export the certificates as environmental variables

      export EC2_PRIVATE_KEY=
      export EC2_CERT=
      export EC2_URL=https://ec2.us-east-1.amazonaws.com
      
    5. Create the security groups

      ec2-create-group loadbalancer-sg -d "Loadbalancer Test group"
      ec2-authorize loadbalancer-sg -o loadbalancer-sg -u $aws_account
      ec2-authorize loadbalancer-sg -p 80 -s 0.0.0.0/0
      
    6. Create the user-data-file for the instance so that apache is started and the index.html file is created

      mkdir -p ~/temp/ 
      echo '#! /bin/sh
      yum -qy install httpd
      touch /var/www/html/index.html
      /etc/init.d/httpd start' > ~/temp/user-data.sh
      
    7. Start the new instance and save the instanceid

      instanceid=`ec2-run-instances ami-31814f58 -k "$keypair" -t t1.micro -g loadbalancer-sg -g default -z us-east-1a -f ~/temp/user-data.sh | grep INSTANCE | awk '{ print $2 }'`
      
    8. Create the loadbalancer and attach the instance

      elb-create-lb test-lb --availability-zones us-east-1a --listener "protocol=http, lb-port=80, instance-port=80"
      elb-register-instances-with-lb test-lb --instances $instanceid
      
    9. Wait until your instance state in the loabalancer is "InService" and try to access the urls

提交回复
热议问题