Hi there, I am trying to create a Cloudfront distr...
# help
k
Hi there, I am trying to create a Cloudfront distribution for media files (e.x a user uploads to s3 using signed urls) and I want to make that available through
<http://media.mysite.com/|media.mysite.com/>...
, I was trying to use the
StaticSite
as an example but for some reason the
DnsValidatedCertificate
is giving me an error, it is trying to find a lambda function in
lambda.Code.fromAsset(path.resolve(__dirname, '..', 'lambda-packages', 'dns_validated_certificate_handler', 'lib')),
looking at the source code and because of that
sst start
is giving me an error
Copy code
Error: Cannot find asset at /Users/path/to/my/app/.build/lambda-packages/dns_validated_certificate_handler/lib
I fixed this by using
Copy code
const acmCertificate = new acm.Certificate(this, 'Certificate', {
      domainName: domain,
      validation: acm.CertificateValidation.fromDns(hostedZone),
    });
instead of
DnsValidatedCertificate
Ok this is not solving the full issue because this does not allow setting the region to us-east-1 😅
f
Hey @Kujtim Hoxha, can you show me what you have rn?
k
@Frank what do you mean by
rn
?
r
Probably just short for "right now"
k
Oh 🤦‍♂️ I will post soon not near my laptop now thanks @Ross Coundon
Copy code
const mediaBucket = new Bucket(this, 'MediaBucket', {
      s3Bucket: {
        removalPolicy: process.env.IS_LOCAL
          ? RemovalPolicy.DESTROY
          : RemovalPolicy.RETAIN,
        cors: [
          {
            allowedHeaders: ['*'],
            allowedMethods: [
              HttpMethods.GET,
              HttpMethods.PUT,
              HttpMethods.HEAD,
            ],
            allowedOrigins: ['*'],
          },
        ],
      },
    });

    const hostedZone = route53.HostedZone.fromLookup(this, 'HostedZone', {
      domainName: '<http://mydomain.com|mydomain.com>',
    });
    let domain = `${this.stage}-<http://media.mydomain.com|media.mydomain.com>`;
    if (this.stage === 'prod') {
      domain = '<http://media.mydomain.com|media.mydomain.com>';
    }
    const acmCertificate = new acm.DnsValidatedCertificate(
      this,
      'Certificate',
      {
        domainName: domain,
        hostedZone: hostedZone,
        region: 'us-east-1',
      }
    );

    const cfDistribution = new cf.Distribution(this, 'Distribution', {
      errorResponses: [],
      // these values can NOT be overwritten by cfDistributionProps
      domainNames: [domain],
      certificate: acmCertificate,
      defaultBehavior: {
        origin: new cfOrigins.S3Origin(mediaBucket.s3Bucket),
        viewerProtocolPolicy: cf.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
      },
    });

    new route53.ARecord(this, 'AliasRecord', {
      recordName: domain,
      zone: hostedZone,
      target: route53.RecordTarget.fromAlias(
        new route53Targets.CloudFrontTarget(cfDistribution)
      ),
    });
Here is what I have until now
f
Thanks @Kujtim Hoxha, and what’s the latest error you are getting with this?
k
The one above
In the main thread
f
Copy code
Error: Cannot find asset at /Users/path/to/my/app/.build/lambda-packages/dns_validated_certificate_handler/lib
when you run
sst start
right?
k
Yep
f
Cool, taking a look.
k
Thanks
I am using yarn workspaces might be related 🤷‍♂️
f
Yeah I just tried the stack in a non-yarn workspace setup and it worked.
Can you share what your yarn workspace structure is like?
k
sure
Copy code
-> backend
---->lib
------->stacks...
---->sst.son
-> frontend
-> common
and I was running yarn commands within the
backend
folder
f
sst.json
is inside the
backend
folder right?
k
correct
I am suspecting it has something to do with the
node_modules
folder being one level above
^ this is causing the error
The weird thing is that StaticSite uses the same construct and it works 😅
O wow I think I fixed it 😮 looks like I need to add
@aws-cdk/aws-certificatemanager
in my package.json, but the weird thing is that for other
@aws-dev/*
I never had to do it 🤷‍♂️
f
Ah
k
I looked in the node_modules and it was there because sst has it as a requirement but to make this work you need to explicitly have it in 🤷‍♂️
I do not really get why
f
when u didn’t put it, is
@aws-cdk/aws-certificatemanager
directly inside
/node_modules
or is it in
/node_modules/@serverless-stack/resources/node_modules/@aws-cdk/aws-certificatemanager
?
k
it all is directly there
But because I am using workspaces it is at the top level node_module
f
Yeah I see it.. just managed to reproduce the issue
k
Thanks for the help Frank, it is all working now 🎉