简体   繁体   English

放大构建失败并显示“请求失败 401 未经授权”

[英]Amplify build fails with "Request failed 401 Unauthorized"

I am building an amplify react app and trying to connect it to my private npm packages in my CodeArtifact repository.我正在构建一个 amplify react 应用程序并尝试将它连接到我的 CodeArtifact 存储库中的私有 npm 包。

In the build file amplify.yml , I added在构建文件amplify.yml ,我添加了

preBuild:
      commands:
        - aws codeartifact login --tool npm --repository myrepo --domain mydomain --namespace mynamespace --domain-owner myid
        - yarn install

and gave the amplify service role the following policy:并为放大服务角色提供以下策略:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codeartifact:GetAuthorizationToken",
                "codeartifact:GetRepositoryEndpoint",
                "codeartifact:ReadFromRepository"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": "sts:GetServiceBearerToken",
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "sts:AWSServiceName": "codeartifact.amazonaws.com"
                }
            }
        }
    ]
}

This setup works for CodeBuild building Lambda functions, but in Amplify, I get此设置适用于构建 Lambda 函数的 CodeBuild,但在 Amplify 中,我得到

Successfully configured npm to use AWS CodeArtifact repository

after the login command and在登录命令和

error An unexpected error occurred: "<some-package-url>: Request failed \\"401 Unauthorized\\"".

when installing dependencies.安装依赖项时。

I debugged the environment in amplify build and did not find any AWS access key id or secret, but also don't know why.我在 amplify build 中调试环境,没有找到任何 AWS 访问密钥 id 或 secret,但也不知道为什么。

Ok I resolved my issue by deleting yarn.lock and adding it to .gitignore .好的,我通过删除yarn.lock并将其添加到.gitignore解决了我的问题。

The problem was, that yarn caches the resolved package address in yarn.lock .问题是, yarn 将解析的包地址缓存在yarn.lock That address was in my CodeArtifact repository, because I was logged in while installing dependencies on my dev machine.该地址在我的 CodeArtifact 存储库中,因为我是在我的开发机器上安装依赖项时登录的。 Since yarn.lock is not in .gitignore by default, I just pushed it into the build.由于默认情况下yarn.lock不在.gitignore ,我只是将其推入构建中。 When yarn installs dependencies in build, it uses the cached addresses, which can't be reached anymore.当yarn 在构建中安装依赖项时,它会使用无法再访问的缓存地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM