Amplify AccessDenied for specific routes

Viewed 141

I have a Nuxt site hosted in Amplify which displays the following AccessDenied page for certain routes:

<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>TCHGPECCM8WBEHSM</RequestId>
<HostId>fc9Jmfxb4sc+T/e8bT2wIQ/W5fT5Li868ydiaU04rQ54AQHLGkh+Nx4cLKufjBczDeLBLrvEJwo=</HostId>
</Error>

A route where this occurs is the following: https://ryanachten.com/projects/echo/, but doesn't occur on other pages such as https://ryanachten.com/projects/whosagoodboy/.

For pages where it does occur, accessing the page by the projects index seems to work https://ryanachten.com/projects but then subsequent refreshes of the page result in the same issue.

The Terraform for the Amplify setup can be found here. Of specific relevance is the Amplify app resource declared as follows:

resource "aws_amplify_app" "ryanachten" {
  name         = "ryanachten"
  repository   = "https://github.com/ryanachten/site"
  access_token = var.github_access_token

  build_spec = <<-EOT
    version: 0.1
    frontend:
      phases:
        preBuild:
          commands:
            - yarn install
        build:
          commands:
            - yarn run generate
      artifacts:
        baseDirectory: dist
        files:
          - '**/*'
      cache:
        paths:
          - node_modules/**/*
  EOT
}

Anyone have any idea why this is occurring?

1 Answers

A second look at the Nuxt generate output indicates what the issue is - only some of the projects are actually being discovered by the Nuxt crawler:

generate output

So the real issue is probably why these dynamic routes are not being discovered and outputted for SSG.

Once this is resolved, I'll make sure that the AccessDenied error is fixed by this change and mark this question as solved.

Update

As I suspected, this was caused by routes not being statically generated. Resolved this by ensuring the routes were generated at build time via declaring them in nuxt.config.js. The yaml being requested here already contained the list of routes in question.

  generate: {
    routes() {
      // Crawler doesn't seem to pick up all the projects
      // so we need to request them to be generated explicitly
      const yamlFile = yaml.load(
        fs.readFileSync(`./content/projects/index.yml`, 'utf8')
      )
      const projects = yamlFile.projects
      return projects.map((p) => `/projects/${p.name.toLowerCase()}`)
    },
  },
Related