Skip to content

Commit 896ba4b

Browse files
Fix scenario tests failing in non-default region (#9149)
1 parent e1ee84e commit 896ba4b

File tree

8 files changed

+248
-520
lines changed

8 files changed

+248
-520
lines changed

tests/aws/cdk_templates/Bookstore/BookstoreStack.json

Lines changed: 193 additions & 464 deletions
Large diffs are not rendered by default.

tests/aws/scenario/bookstore/functions/getBook.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
"use strict";
44

55
const AWS = require("aws-sdk");
6-
let dynamoDb;
6+
7+
var config = {};
78
if (process.env.AWS_ENDPOINT_URL) {
8-
dynamoDb = new AWS.DynamoDB.DocumentClient({
9-
endpoint: process.env.AWS_ENDPOINT_URL,
10-
region: 'us-east-1', // Change the region as per your setup
11-
}
12-
);
13-
} else {
14-
dynamoDb = new AWS.DynamoDB.DocumentClient();
9+
config.endpoint = process.env.AWS_ENDPOINT_URL;
1510
}
11+
12+
let dynamoDb = new AWS.DynamoDB.DocumentClient(config);
13+
1614
// GetBook - Get book informaton for a given book id
1715
exports.handler = (event, context, callback) => {
1816

tests/aws/scenario/bookstore/functions/listBooks.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
"use strict";
44

55
const AWS = require("aws-sdk");
6-
let dynamoDb;
6+
7+
var config = {};
78
if (process.env.AWS_ENDPOINT_URL) {
8-
dynamoDb = new AWS.DynamoDB.DocumentClient({
9-
endpoint: process.env.AWS_ENDPOINT_URL,
10-
region: 'us-east-1', // Change the region as per your setup
11-
}
12-
);
13-
} else {
14-
dynamoDb = new AWS.DynamoDB.DocumentClient();
9+
config.endpoint = process.env.AWS_ENDPOINT_URL;
1510
}
11+
12+
let dynamoDb = new AWS.DynamoDB.DocumentClient(config);
13+
1614
// ListBooks - List all books or list all books in a particular category
1715
exports.handler = (event, context, callback) => {
1816

tests/aws/scenario/bookstore/functions/loadBooksHelper.js

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
11
// source: https://github.com/aws-samples/aws-bookstore-demo-app/blob/master/functions/setup/uploadBooks.js
2+
23
"use strict";
34

45
const https = require("https");
56
const url = require("url");
7+
const AWS = require("aws-sdk");
68

7-
var AWS = require("aws-sdk");
8-
let documentClient;
9-
let s3Client;
9+
var config = {
10+
's3ForcePathStyle': true,
11+
};
1012
if (process.env.AWS_ENDPOINT_URL) {
11-
const localStackS3Config = {
12-
endpoint: process.env.AWS_ENDPOINT_URL,
13-
s3ForcePathStyle: true,
14-
accessKeyId: 'test',
15-
secretAccessKey: 'test',
16-
region: 'us-east-1',
17-
};
18-
s3Client = new AWS.S3(localStackS3Config);
19-
20-
documentClient = new AWS.DynamoDB.DocumentClient({
21-
endpoint: process.env.AWS_ENDPOINT_URL,
22-
region: 'us-east-1', // Change the region as per your setup
23-
}
24-
);
25-
} else {
26-
// Use the default AWS configuration
27-
s3Client = new AWS.S3();
28-
documentClient = new AWS.DynamoDB.DocumentClient();
13+
config.endpoint = process.env.AWS_ENDPOINT_URL;
2914
}
3015

16+
let documentClient = new AWS.DynamoDB.DocumentClient(config);
17+
let s3Client = new AWS.S3(config);
18+
3119
// UploadBooks - Upload sample set of books to DynamoDB
3220
exports.handler = function(event, context, callback) {
3321
getBooksData().then(function(data) {

tests/aws/scenario/bookstore/functions/search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import requests
77
from requests_aws4auth import AWS4Auth
88

9-
region = os.environ["REGION"]
9+
region = os.environ["AWS_REGION"]
1010
service = "es"
1111
credentials = boto3.Session().get_credentials()
1212
awsauth = AWS4Auth(

tests/aws/scenario/bookstore/functions/update_search_cluster.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import requests
77
from requests_aws4auth import AWS4Auth
88

9-
region = os.environ["REGION"]
9+
region = os.environ["AWS_REGION"]
1010
service = "es"
1111
credentials = boto3.Session().get_credentials()
1212
awsauth = AWS4Auth(

tests/aws/scenario/bookstore/test_bookstore.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
import aws_cdk.aws_opensearchservice as opensearch
1111
import pytest
1212
from aws_cdk.aws_lambda_event_sources import DynamoEventSource
13+
from botocore.exceptions import ClientError
1314
from constructs import Construct
1415

1516
from localstack.testing.pytest import markers
1617
from localstack.testing.scenario.cdk_lambda_helper import load_python_lambda_to_s3
1718
from localstack.testing.scenario.provisioning import InfraProvisioner, cleanup_s3_bucket
1819
from localstack.testing.snapshots.transformer import GenericTransformer, KeyValueBasedTransformer
20+
from localstack.utils.aws.resources import create_s3_bucket
1921
from localstack.utils.files import load_file
2022
from localstack.utils.strings import to_bytes, to_str
2123
from localstack.utils.sync import retry
@@ -115,7 +117,11 @@ def test_setup(self, aws_client, infrastructure, snapshot, cleanups):
115117

116118
# pre-fill dynamodb
117119
# json-data is from https://aws-bookstore-demo.s3.amazonaws.com/data/books.json
118-
aws_client.s3.create_bucket(Bucket=S3_BUCKET_BOOKS_INIT)
120+
try:
121+
create_s3_bucket(bucket_name=S3_BUCKET_BOOKS_INIT, s3_client=aws_client.s3)
122+
except ClientError as exc:
123+
if exc.response["Error"]["Code"] != "BucketAlreadyOwnedByYou":
124+
raise exc
119125
cleanups.append(
120126
lambda: cleanup_s3_bucket(
121127
aws_client.s3, bucket_name=S3_BUCKET_BOOKS_INIT, delete_bucket=True
@@ -410,6 +416,16 @@ def __init__(
410416
projection_type=dynamodb.ProjectionType.ALL,
411417
)
412418

419+
self.lambda_role = iam.Role(
420+
self, "LambdaRole", assumed_by=iam.ServicePrincipal("lambda.amazonaws.com")
421+
)
422+
self.lambda_role.add_managed_policy(
423+
iam.ManagedPolicy.from_aws_managed_policy_name("AmazonS3FullAccess")
424+
)
425+
self.lambda_role.add_managed_policy(
426+
iam.ManagedPolicy.from_aws_managed_policy_name("AmazonDynamoDBFullAccess")
427+
)
428+
413429
# lambda for pre-filling the dynamodb
414430
self.load_books_helper_fn = awslambda.Function(
415431
stack,
@@ -422,24 +438,20 @@ def __init__(
422438
"S3_BUCKET": S3_BUCKET_BOOKS_INIT,
423439
"FILE_NAME": S3_KEY_BOOKS_INIT,
424440
},
441+
role=self.lambda_role,
425442
)
426-
self.load_books_helper_fn.role.attach_inline_policy(
427-
iam.Policy(
428-
stack,
429-
"BooksS3Policy",
430-
statements=[
431-
iam.PolicyStatement(resources=["arn:aws:s3:::*/*"], actions=["s3:GetObject"])
432-
],
433-
)
434-
)
443+
435444
# lambdas to get and list books
436445
self.get_book_fn = awslambda.Function(
437446
stack,
438447
"GetBookLambda",
439448
handler="index.handler",
440449
code=awslambda.InlineCode(code=load_file(self.GET_BOOK_PATH)),
441450
runtime=awslambda.Runtime.NODEJS_16_X,
442-
environment={"TABLE_NAME": self.books_table.table_name},
451+
environment={
452+
"TABLE_NAME": self.books_table.table_name,
453+
},
454+
role=self.lambda_role,
443455
)
444456

445457
self.list_books_fn = awslambda.Function(
@@ -448,7 +460,10 @@ def __init__(
448460
handler="index.handler",
449461
code=awslambda.InlineCode(code=load_file(self.LIST_BOOKS_PATH)),
450462
runtime=awslambda.Runtime.NODEJS_16_X,
451-
environment={"TABLE_NAME": self.books_table.table_name},
463+
environment={
464+
"TABLE_NAME": self.books_table.table_name,
465+
},
466+
role=self.lambda_role,
452467
)
453468

454469
# lambda to search for book
@@ -465,8 +480,8 @@ def __init__(
465480
runtime=awslambda.Runtime.PYTHON_3_10,
466481
environment={
467482
"ESENDPOINT": self.opensearch_domain.domain_endpoint,
468-
"REGION": stack.region,
469483
},
484+
role=self.lambda_role,
470485
)
471486

472487
# lambda to update search cluster
@@ -478,8 +493,8 @@ def __init__(
478493
runtime=awslambda.Runtime.PYTHON_3_10,
479494
environment={
480495
"ESENDPOINT": self.opensearch_domain.domain_endpoint,
481-
"REGION": stack.region,
482496
},
497+
role=self.lambda_role,
483498
)
484499

485500
event_source = DynamoEventSource(
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"tests/aws/scenario/bookstore/test_bookstore.py::TestBookstoreApplication::test_lambda_dynamodb": {
3-
"last_validated_date": "2023-08-30T11:23:16+00:00"
3+
"last_validated_date": "2024-01-31T13:47:03+00:00"
44
},
55
"tests/aws/scenario/bookstore/test_bookstore.py::TestBookstoreApplication::test_opensearch_crud": {
66
"last_validated_date": "2023-09-07T10:02:59+00:00"
77
},
88
"tests/aws/scenario/bookstore/test_bookstore.py::TestBookstoreApplication::test_search_books": {
9-
"last_validated_date": "2023-08-31T11:39:41+00:00"
9+
"last_validated_date": "2024-01-31T13:47:48+00:00"
1010
},
1111
"tests/aws/scenario/bookstore/test_bookstore.py::TestBookstoreApplication::test_setup": {
12-
"last_validated_date": "2023-08-29T15:20:44+00:00"
12+
"last_validated_date": "2024-01-31T13:46:54+00:00"
1313
}
1414
}

0 commit comments

Comments
 (0)