Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate More Python Examples #207

Open
skorfmann opened this issue Jul 18, 2020 · 0 comments
Open

Generate More Python Examples #207

skorfmann opened this issue Jul 18, 2020 · 0 comments

Comments

@skorfmann
Copy link
Collaborator

@skorfmann skorfmann commented Jul 18, 2020

jsii-rosetta could be used to generate Python examples from the Typescript examples.

While the typescript-aws example doesn't compile cleanly ("arrow functions are not supported"), it would be easy to fix that. The cloudfront example compiles cleanly.

Expand for generated Python code

from constructs import Construct
from cdktf import App, TerraformStack, TerraformOutput
from ...gen.providers.aws import CloudfrontDistribution, AcmCertificate, Route53Record, AcmCertificateValidation, AwsProvider

class MyStack(TerraformStack):
    def __init__(self, scope, ns):
        super().__init__(scope, ns)

        origin_id = "myS3Origin"
        domain_name = "www.example.com"
        proxy_target = "example.com"

        AwsProvider(self, "aws",
            region="eu-central-1"
        )

        provider = AwsProvider(self, "aws.route53",
            region="us-east-1",
            alias="route53"
        )

        cert = AcmCertificate(self, "cert",
            domain_name=domain_name,
            validation_method="DNS",
            provider=provider
        )

        # const zone = new DataAwsRoute53Zone(this, 'zone', {
        #   name: 'example.com.',
        #   privateZone: false
        # })

        record = Route53Record(self, "CertValidationRecord",
            name=cert.domain_validation_options("0").domain_name,
            type=cert.domain_validation_options("0").resource_record_type,
            records=[
                cert.domain_validation_options("0").resource_record_value
            ],
            # zoneId: zone.zoneId,
            zone_id="123",
            ttl=60,
            allow_overwrite=True
        )

        AcmCertificateValidation(self, "certvalidation",
            certificate_arn=cert.arn,
            validation_record_fqdns=[record.fqdn],
            provider=provider
        )

        distribution = CloudfrontDistribution(self, "cloudfront",
            enabled=True,
            is_ipv6_enabled=True,

            viewer_certificate=[CloudfrontDistributionViewerCertificate(
                acm_certificate_arn=cert.arn,
                ssl_support_method="sni-only"
            )],

            restrictions=[CloudfrontDistributionRestrictions(
                geo_restriction=[CloudfrontDistributionRestrictionsGeoRestriction(
                    restriction_type="none"
                )]
            )],

            origin=[CloudfrontDistributionOrigin(
                origin_id=origin_id,
                domain_name=proxy_target,
                custom_origin_config=[CloudfrontDistributionOriginCustomOriginConfig(
                    http_port=80,
                    https_port=443,
                    origin_protocol_policy="http-only",
                    origin_ssl_protocols=["TLSv1.2", "TLSv1.1"]
                )]
            )],

            aliases=[domain_name
            ],

            default_cache_behavior=[CloudfrontDistributionDefaultCacheBehavior(
                min_ttl=0,
                default_ttl=60,
                max_ttl=86400,
                allowed_methods=["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"],
                cached_methods=["GET", "HEAD"],
                target_origin_id=origin_id,
                viewer_protocol_policy="redirect-to-https",
                forwarded_values=[CloudfrontDistributionDefaultCacheBehaviorForwardedValues(
                    cookies=[CloudfrontDistributionDefaultCacheBehaviorForwardedValuesCookies(
                        forward="all"
                    )],
                    headers=["Host", "Accept-Datetime", "Accept-Encoding", "Accept-Language", "User-Agent", "Referer", "Origin", "X-Forwarded-Host"],
                    query_string=True
                )]
            )]
        )

        Route53Record(self, "distribution_domain",
            name=domain_name,
            type="A",
            # zoneId: zone.zoneId,
            zone_id="123",
            alias=[Route53RecordAlias(
                name=distribution.domain_name,
                zone_id=distribution.hosted_zone_id,
                evaluate_target_health=True
            )]
        )

        TerraformOutput(self, "distribution_domain_name",
            value=distribution.domain_name
        )

app = App()
MyStack(app, "typescript-aws-cloudfront-proxy")
app.synth()

The examples could be restructured a bit so that only Typescript examples are maintained. In an additional build step, Python and later on C# / Java / ... could be automatically generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant