"botocore.exceptions.NoRegionError: You must specify a region." when deploying to ECR
"botocore.exceptions.NoRegionError: You must specify a region." when deploying to ECR
我正在按照 this 教程使用 CDK 部署到 ECS,但是当我部署时,部署后出现此错误
botocore.exceptions.NoRegionError: You must specify a region.
这是我正在部署的内容。
app.py
#!/usr/bin/env python3
from os import environ
from aws_cdk import core as cdk
from ecs_test.ecs_test_stack import EcsTestStack
_env=cdk.Environment(account=environ["CDK_DEFAULT_ACCOUNT"], region='ap-southeast-2')
app = cdk.App()
EcsTestStack(app, "EcsTestStackV3", env=_env)
app.synth()
ecs_test_stack.py
from aws_cdk import core, aws_ecs_patterns, aws_ec2, aws_ecs
class EcsTestStack(core.Stack):
def __init__(self, scope: core.Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
_container_image = aws_ecs.ContainerImage.from_asset(
directory=".",
file='Dockerfile.ECS_Test',
exclude=["cdk.out"]
)
vpc = aws_ec2.Vpc(self, "ecs-test-vpc-v3", max_azs=3)
cluster = aws_ecs.Cluster(self, "ecs-test-cluster-v3", vpc=vpc)
cluster.add_capacity("ecs-autoscaling-capacity-v3",
instance_type=aws_ec2.InstanceType("t2.small"),
min_capacity=1,
max_capacity=3)
self.ecs_test = aws_ecs_patterns.QueueProcessingEc2Service(
self,
"ECS_Test_Pattern_v3",
cluster=cluster,
cpu=512,
memory_limit_mib=512,
image=_container_image,
min_scaling_capacity=1,
max_scaling_capacity=5
)
Dockerfile.ECS_Test
from alpine:3.8
RUN apk add -U python3 py3-pip && pip3 install awscli boto3
COPY ./ecs_test/queue_service.py /queue_service.py
CMD ["/queue_service.py","receive"]
queue_service.py
#!/usr/bin/env python3
from boto3 import resource
from os import getenv
from time import sleep
from random import randrange
from sys import argv
def get_queue_details():
sqs = resource('sqs')
print(getenv('QUEUE_NAME'))
return sqs.get_queue_by_name(QueueName=getenv('QUEUE_NAME'))
def receive():
queue = get_queue_details()
while True:
for message in queue.receive_messages():
print("MESSAGE CONSUMED: {}".format(message.body))
print(message.delete())
sleep(1)
def send(num_messages=100):
queue = get_queue_details()
for _num in range(num_messages):
_rand = randrange(1000)
print(queue.send_message(MessageBody=str(_rand)))
if __name__ == '__main__':
try:
if argv[1] == 'send':
send()
if argv[1] == 'receive':
receive()
except IndexError as I:
print("Please pass either send or receive.\n./queue_service.py <send> <receive>")
exit(200)
queue_service.py 在我的系统上本地运行时运行良好,但是当我使用 CDK 部署并填充我的队列时,我收到 botocore.exceptions.NoRegionError: You must specify a region.
错误。
问题:
如何在我的 ECS 上设置区域?
您必须告知Boto3您要在哪个地区使用sqs资源。
在 queue_service.py 中为 sqs 资源设置 region_name
。
sqs = resource('sqs', region_name='us-west-2')
或
在queue_service.py
中设置AWS_DEFAULT_REGION
环境变量
os.environ['AWS_DEFAULT_REGION'] = 'us-west-2'
或
在Dockerfile
中设置AWS_DEFAULT_REGION
环境变量
ENV AWS_DEFAULT_REGION=us-west-2
或
在ECS task definition
中设置环境变量
我正在按照 this 教程使用 CDK 部署到 ECS,但是当我部署时,部署后出现此错误
botocore.exceptions.NoRegionError: You must specify a region.
这是我正在部署的内容。
app.py
#!/usr/bin/env python3
from os import environ
from aws_cdk import core as cdk
from ecs_test.ecs_test_stack import EcsTestStack
_env=cdk.Environment(account=environ["CDK_DEFAULT_ACCOUNT"], region='ap-southeast-2')
app = cdk.App()
EcsTestStack(app, "EcsTestStackV3", env=_env)
app.synth()
ecs_test_stack.py
from aws_cdk import core, aws_ecs_patterns, aws_ec2, aws_ecs
class EcsTestStack(core.Stack):
def __init__(self, scope: core.Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
_container_image = aws_ecs.ContainerImage.from_asset(
directory=".",
file='Dockerfile.ECS_Test',
exclude=["cdk.out"]
)
vpc = aws_ec2.Vpc(self, "ecs-test-vpc-v3", max_azs=3)
cluster = aws_ecs.Cluster(self, "ecs-test-cluster-v3", vpc=vpc)
cluster.add_capacity("ecs-autoscaling-capacity-v3",
instance_type=aws_ec2.InstanceType("t2.small"),
min_capacity=1,
max_capacity=3)
self.ecs_test = aws_ecs_patterns.QueueProcessingEc2Service(
self,
"ECS_Test_Pattern_v3",
cluster=cluster,
cpu=512,
memory_limit_mib=512,
image=_container_image,
min_scaling_capacity=1,
max_scaling_capacity=5
)
Dockerfile.ECS_Test
from alpine:3.8
RUN apk add -U python3 py3-pip && pip3 install awscli boto3
COPY ./ecs_test/queue_service.py /queue_service.py
CMD ["/queue_service.py","receive"]
queue_service.py
#!/usr/bin/env python3
from boto3 import resource
from os import getenv
from time import sleep
from random import randrange
from sys import argv
def get_queue_details():
sqs = resource('sqs')
print(getenv('QUEUE_NAME'))
return sqs.get_queue_by_name(QueueName=getenv('QUEUE_NAME'))
def receive():
queue = get_queue_details()
while True:
for message in queue.receive_messages():
print("MESSAGE CONSUMED: {}".format(message.body))
print(message.delete())
sleep(1)
def send(num_messages=100):
queue = get_queue_details()
for _num in range(num_messages):
_rand = randrange(1000)
print(queue.send_message(MessageBody=str(_rand)))
if __name__ == '__main__':
try:
if argv[1] == 'send':
send()
if argv[1] == 'receive':
receive()
except IndexError as I:
print("Please pass either send or receive.\n./queue_service.py <send> <receive>")
exit(200)
queue_service.py 在我的系统上本地运行时运行良好,但是当我使用 CDK 部署并填充我的队列时,我收到 botocore.exceptions.NoRegionError: You must specify a region.
错误。
问题: 如何在我的 ECS 上设置区域?
您必须告知Boto3您要在哪个地区使用sqs资源。
在 queue_service.py 中为 sqs 资源设置 region_name
。
sqs = resource('sqs', region_name='us-west-2')
或
在queue_service.py
中设置AWS_DEFAULT_REGION
环境变量
os.environ['AWS_DEFAULT_REGION'] = 'us-west-2'
或
在Dockerfile
AWS_DEFAULT_REGION
环境变量
ENV AWS_DEFAULT_REGION=us-west-2
或
在ECS task definition