Search code examples
amazon-web-servicesboto3amazon-ecsaws-cdk

"botocore.exceptions.NoRegionError: You must specify a region." when deploying to ECR


I am following this tutorial to deploy to ECS using CDK, but when I deploy I get this error after deployment

botocore.exceptions.NoRegionError: You must specify a region.

Here is what I am deploying.

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 works well when it runs locally on my system, but when I deploy using CDK and fill my queue up, I get the botocore.exceptions.NoRegionError: You must specify a region. error.

Question: How do I set the region on my ECS?


Solution

  • You must inform Boto3 in which region you want to use the sqs resource.

    Set region_name in queue_service.py for sqs resource.

    sqs = resource('sqs', region_name='us-west-2')
    

    OR

    set AWS_DEFAULT_REGION environment variable in queue_service.py

    os.environ['AWS_DEFAULT_REGION'] = 'us-west-2'
    

    OR

    Set AWS_DEFAULT_REGION environment variable in Dockerfile

    ENV AWS_DEFAULT_REGION=us-west-2
    

    OR

    Set environment variable in ECS task definition