Created
May 13, 2022 14:41
-
-
Save thorin-schiffer/c2ec98cdf0770ef0efd78fd63f12ba45 to your computer and use it in GitHub Desktop.
CDK/Cloudformation and moto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@pytest.fixture(scope="session") | |
def cdk(ssm_mock, s3_mock, sqs_mock): | |
with open(Path(os.path.dirname(__file__)) / "resources" / "template.yaml") as f: | |
template = f.read() | |
parsed = yaml.load(template) | |
assets_bucket_name = None | |
lambda_zips = {} | |
for name, spec in parsed["Resources"].items(): | |
spec["Properties"]["ApiKeyRequired"] = False | |
spec["Properties"]["StageName"] = "test" | |
if spec["Type"] == "AWS::Lambda::Function": | |
assets_bucket_name = assets_bucket_name or spec["Properties"]["Code"]["S3Bucket"] | |
lambda_zips[name] = spec["Properties"]["Code"]["S3Key"] | |
fixed_template = yaml.dump(parsed) | |
# fixing moto param parsing, fails with no variable found | |
ssm_mock.put_parameter(Name=parsed["Parameters"]["BootstrapVersion"]["Default"], Value="1") | |
s3_mock.create_bucket(Bucket=assets_bucket_name, CreateBucketConfiguration={"LocationConstraint": "eu-central-1"}) | |
for name, key in lambda_zips.items(): | |
f = BytesIO() | |
z = zipfile.ZipFile(f, "w") | |
z.writestr("lambda_function.py", 'def lambda_handler(): print("test")') | |
z.close() | |
s3_mock.put_object(Body=f.getvalue(), Bucket=assets_bucket_name, Key=key) | |
f.close() | |
with mock_cloudformation(): | |
import boto3 | |
cf_mock = boto3.client("cloudformation", region_name="eu-central-1") | |
cf_mock.create_stack(StackName="Test", TemplateBody=fixed_template) | |
yield |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment