是否可以通过Elastic Beanstalk使用多个外部IP地址? [英] Is there a way to have multiple external IP addresses with Elastic Beanstalk?

查看:78
本文介绍了是否可以通过Elastic Beanstalk使用多个外部IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Amazon Elastic Beanstalk与VPC一起使用,我想拥有多个具有不同IP地址的环境(工作人员).我不需要它们是静态的,我实际上希望它们在可能的情况下定期更改.

I'm using Amazon Elastic Beanstalk with a VPC and I want to have multiple environments (workers) with different IP addresses. I don't need them to be static, I would actually prefer them to change regularly if possible.

有没有办法让多个环境具有动态外部IP地址?

Is there a way to have multiple environments with dynamic external IP addresses?

推荐答案

很难理解想要更改Elastic Beanstalk环境的实例IP地址的用例.像Elastic Beanstalk这样的托管服务提供的基本优势是对用于部署的基础架构的抽象.您将获得一个CNAME来访问环境(您的应用程序)的API,并且您不应依赖内部IP地址或负载均衡器URL来添加任何内容,因为它们可以被beantalk服务随意添加或删除.

It's hard to understand the use case of wanting to change the instance IP address of an Elastic Beanstalk environment. The fundamental advantage that a managed service like Elastic Beanstalk provides is abstraction over the underlying architecture for a deployment. You are given a CNAME to access the environment's (your application's) API and you shouldn't be relying on the internal IP addresses or Load Balancer URLs for anything as they can be added, removed by the beanstalk service at will.

话虽如此,您有一种方法可以实现更改基础实例的IP.

That being said, there is a way that you can achieve having changing IPs for the underlying instances.

Elastic Beanstalk <代码>重建环境破坏包括EC2在内的现有资源,并创建新资源,从而使您的实例具有新IP地址.鉴于计划的停机时间(几分钟,具体取决于您的资源)对于此用例而言不是问题,因此可以正常工作.

Elastic Beanstalk Rebuild Environment destroys the existing resources including EC2s and creates new resources resulting in your instances having new IP addresses. This would work given that a scheduled downtime (of a few minutes depending on your resources) is not a problem for this use case.

您可以使用以下两种方法之一来计划环境重建

You can use one the following two ways to schedule an environment rebuild

解决方案1:

您可以使用简单的lambda函数安排 Rebuild Environment .

You can schedule your Rebuild Environment using a simple lambda function.

import boto3
envid=['e-awsenvidid']
client = boto3.client('elasticbeanstalk')
def handler(event, context):
    try:
         for appid in range(len(envid)):
             response = client.rebuild_environment(EnvironmentId=str(envid[appid].strip()))
             if response:
                 print('Restore environment %s' %str(envid[appid]))
             else:
                 print('Failed to Restore environment %s' %str(envid[appid]))

    except Exception as e:
        print('EnvironmentID is not valid')

为此,您必须创建具有所需权限的IAM角色.

In order to do this you will have to create an IAM role with the required permissions.

您可以在此内容中找到全面的指南.AWS指南.

解决方案2:

您可以使用cron作业通过aws-cli重建环境.您可以按照以下步骤来实现.

You can use a cron job to rebuild the environment using aws-cli. You can follow the steps below to achieve this.

  1. 创建EC2实例
  2. 创建具有重建环境权限的IAM角色

以下示例策略将有效

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "elasticbeanstalk:RebuildEnvironment"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}

  1. 将IAM角色附加到EC2实例
  2. 使用命令 crontab -e
  3. 添加cron作业
  1. Attach the IAM role to the EC2 instance
  2. Add a cron job using command crontab -e

以下示例cron作业在每月的1号凌晨12点重建环境.

The following example cron job rebuilds the environment at 12.00 am on the 1st of every month

0 0 1 * * aws elasticbeanstalk rebuild-environment --environment-name my-environment-name

  1. 保存cronjob并退出.

不建议您不必要地重建环境,但是到目前为止,还没有明确的方法来满足您的特定要求.希望对您有所帮助!

进一步阅读:

这篇关于是否可以通过Elastic Beanstalk使用多个外部IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆