Terraform Gives errors Failed to load plugin schemas - amazon-s3

I have below code which I am using for create s3 bucket and cloud front in aws through terraform but terraform gives error.
I am using latest version of terraform cli exe for windows.
Main.tf
Please find below code of main.tf file :
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "3.70.0"
}
}
}
provider "aws" {
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
region = "${var.aws_region}"
}
resource "aws_s3_bucket" "mybucket" {
bucket = "${var.bucket_name}"
acl = "public-read"
website {
redirect_all_requests_to = "index.html"
}
cors_rule {
allowed_headers = ["*"]
allowed_methods = ["PUT","POST"]
allowed_origins = ["*"]
expose_headers = ["ETag"]
max_age_seconds = 3000
}
policy = <<EOF
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "PublicReadForGetBucketObjects",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::${var.bucket_name}/*"
}
]
}
EOF
}
resource "aws_cloudfront_distribution" "distribution" {
origin {
domain_name = "${aws_s3_bucket.mybucket.website_endpoint}"
origin_id = "S3-${aws_s3_bucket.mybucket.bucket}"
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "match-viewer"
origin_ssl_protocols = ["TLSv1", "TLSv1.1", "TLSv1.2"]
}
}
default_root_object = "index.html"
enabled = true
custom_error_response {
error_caching_min_ttl = 3000
error_code = 404
response_code = 200
response_page_path = "/index.html"
}
default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "S3-${aws_s3_bucket.mybucket.bucket}"
forwarded_values {
query_string = true
cookies {
forward = "none"
}
}
viewer_protocol_policy = "allow-all"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}
# Restricts who is able to access this content
restrictions {
geo_restriction {
# type of restriction, blacklist, whitelist or none
restriction_type = "none"
}
}
# SSL certificate for the service.
viewer_certificate {
cloudfront_default_certificate = true
}
}
Please find below error message:
Error: Failed to load plugin schemas
│
│ Error while loading schemas for plugin components: Failed to obtain provider schema: Could not load the schema for provider registry.terraform.io/hashicorp/aws: failed to retrieve schema
│ from provider "registry.terraform.io/hashicorp/aws": Plugin did not respond: The plugin encountered an error, and failed to respond to the plugin.(*GRPCProvider).GetProviderSchema call. The
│ plugin logs may contain more details...
Please help to resolve the issue , I'm new in terraforms.
P.S. This error generated while terraform plan

I had faced the same issue. The error was a little different tho. I was following the HashiCups provider example.
It happens when initializing it fails to upgrade or detect corrupt cached providers, or if you have changed the version but when you only run terraform init it finds the version in cache and decides to go with it. Delete the terraform directory and lock file, and then init again terraform init -upgrade
If you're on running it on Apple M1 chip, you might as well need to set this:
export GODEBUG=asyncpreemptoff=1;
https://discuss.hashicorp.com/t/terraform-aws-provider-panic-plugin-did-not-respond/23396
https://github.com/hashicorp/terraform/issues/26104

I'm a beginner at terraform and I had same problem, so I hope this can help. I had same error "Failed to load plugin schemas" and I made a mess with .terraform.lock.hcl .terraform files.
Here's what I did
I created new project at the path where I installed terraform and aws, and it was at “desktop” ..
I don't know if that was necessary or not.
Then I created new two files provider.tf, and main.tf then I typed in terminal terraform init
after that, it will automatically generate those files:
.terraform
.terraform.lock.hcl
and I didn't change something on them. then run terraform plan in terminal.
here's my provider.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
provider "aws" {
region = "us-east-1"
shared_config_files = ["~/.aws/config"]
shared_credentials_files = ["~/.aws/credentials"]
profile = "terraform-user"
}
and here's main.tf
resource "aws_vpc" "vpc" {
cidr_block = "10.123.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "vpc"
}
}
here's some other problems you may face too:
after you create provider.tf and main.tf then after running terraform init in terminal you get "no change", then you should save files before you run the commands in terminal.

I had a similar problem today, where I was getting the same error with terraform plan, apply or destroy commands.
After looking for easy solutions in vain, I decided to run terraform init and it solved the error. I was able to run terraform destroy to successfully destroy 52 resources. Phew!

I was facing the same problem and for me the issue was that I was running terraform plan from /home where the partition mount point had "noexec" enabled.
You can simply run your terraform from somewhere else or disable the "noexec" from current mount point:
vi etc/fstab to edit and remove the noexec flag, change
/dev/mapper/VG00-LVhome /home ext4 defaults,noexec,nosuid
To
/dev/mapper/VG00-LVhome /home ext4 defaults,nosuid
And remount /home with mount -o remount /home
I hope it helps.

I also got this issue too. I previously used Terraform's time_static resource (link here), ran an apply, then no longer needed it and removed it from my Terraform code, then tried to run a plan and got this error.
What fixed it for me was running terraform state list, finding the time_static resource in my Terraform state, then terraform state rm on the time_static resource, then deleted my .terraform directory, ran terraform init then terraform plan and this worked

Related

Error in gitlab-ci when trying to use hashicorp/dir/template module

I'm getting the error that base_dir is an invalid argument. But according to the documentation on the hashicorp repo, it's a required string.
Here's my main.tf:
terraform {
backend "http" {}
required_providers {
gitlab = {
source = "gitlabhq/gitlab"
version = "~> 3.1"
}
aws = {
source = "hashicorp/aws"
version = "~> 3.27"
}
}
required_version = ">= 0.14.9"
}
module "template_files" {
source = "hashicorp/dir/template"
base_dir = "${path.module}/../frontend"
}
Any ideas as to why gitlab would not be able to see the module properly? I'm just trying to get my files into the s3 bucket, but I can't even get a plan to run yet. Thanks in advance!
Today I faced the same issue. To resolve the issue I removed .terraform/ and update the package to 1.3.1 (I have started in 1.2.9 but changed to 1.3.1, when the issue started). After removed the folder, ran terraform init again and configured the CI image for hashicorp/terraform:1.3.1.
Worked for me, I hope the same to you

How to use a CloudFlare Worker Environment Variable in Quasar? (Vue)

Hello & Happy New Year!
I am facing a problem in my Quasar (v1) app in regard to environment variables. I don't want to solve this using dotenv or any other Node package as these are no longer the recommended way to use environment variables in Quasar.
So I have some local environment variables I am setting/using, following the documented best practice on the Quasar official docs:
quasar.conf.js:
build: {
env: {
EXAMPLE: ctx.dev
? JSON.stringify('https://dev.')
: JSON.stringify('https://prod.')
},
This allows me to specify a different endpoint in dev and prod as I would expect, but not ideal for a few obvious reasons.
index.vue:
console.log(process.env.EXAMPLE,'<---API')
I get the expected output of my mock API endpoint. Good.
Now CloudFlare Workers' variables are globally scoped (No process.env object), so once configured in wrangler.toml, it should be possible to simply call them by name:
wrangler.toml:
[vars]
CFEXAMPLE = "example_token"
BUT this does not work (I cannot get my Quasar application to build if I include this as follows). Probably because CFEXAMPLE is not defined in my quasar.conf.js
quasar.conf.js:
build: {
env: {
CFEXAMPLE: CFEXAMPLE
},
I also cannot console.log CFEXAMPLE from my index.vue file either (but I CAN build my app and deploy to CloudFlare OK).
What is the best way to get environment variables working correctly across CloudFlare and localhost please?
Thanks
Ok so thanks to Michal for the hint. In the end I did this:
Firstly create an environment.js.
environment.js:
module.exports = {
dev:{
NODE_ENV: 'development',
PROXY_URL: 'xx',
}
},
prod:{
NODE_ENV: 'production',
PROXY_URL: 'zz',
}
}
}
Next import it and include a function to return the correct environmental var.
quasar.conf.js:
const config = require('./src/boot/environment.js')
module.exports = function(ctx) {
const getEnvVar = p => {
if (ctx.dev) return (config.dev[p])
else return (config.prod[p])
}
...
Finally add environment variables to env:
quasar.conf.js:
env:{
PROXY_URL:JSON.stringify(getEnvVar('PROXY_URL')),
POST_TO:JSON.stringify(getEnvVar('POST_TO')),
FIRESTORE_CREDS:JSON.stringify(getEnvVar('FIRESTORE_CREDS')),
},

What should be used for endpoint in renovate-bot config.json?

I am trying to set up config.json for Bitbucket Cloud to automatically update dependencies in npm repos of Bitbucket Cloud. I found one example, but cannot figure out two things:
endpoint - what should go there (ABC)? - our company's bitbucket namespace link looks like: https://bitbucket.org/uvxyz/
Can I use renovate-bot to issue PRs without bitbucket pipelines? If so, can I make renovate to update only particular repo or repos via config.json mods or I shall put renovate.json file in each repo where automatic dependency update is required?
appreciate any examples on the latter.
config.json:
module.exports = {
"platform": "bitbucket",
"username": "<my.username>",
"password": "<bitbucket token on my account>",
"endpoint": "ABC",
"hostRules": [
{
"hostType": "bitbucket",
"domainName": "ABC",
"timeout": 10000,
"username": "<my.username>",
"password": "<bitbucket token on my account>"
}
]
};
according to the code:
const BITBUCKET_PROD_ENDPOINT = 'https://api.bitbucket.org/';
const defaults = { endpoint: BITBUCKET_PROD_ENDPOINT };
there's a default, it works for me without setting it
what you see in the documentation is all you need
I was able to get renovate working with BB after putting the following configuration into its config.js file as
{
hostType: 'bitbucket',
matchHost: 'https://api.bitbucket.org/2.0/',
username: "bb-username",
password: "<special app password generated for bb-username>",
}
for BitBucket app passwords please look at
https://support.atlassian.com/bitbucket-cloud/docs/create-an-app-password/
and
https://support.atlassian.com/bitbucket-cloud/docs/app-passwords/

Environment variable in Vercel redirects

I want to deploy my create-react-app to Vercel.
I define my redirects in my now.json as follows:
{
"redirects": [
{ "source": "/api/(.*)", "destination": "BACKEND_URL/$1", "statusCode": 200 }
]
}
The destination URL depends on the environment variable BACKEND_URL, which is defined in the Vercel dashboard.
I am trying to replace the environment variable in the redirects in the following build command:
sed -i "s|BACKEND_URL|${BACKEND_URL}|g" now.json && yarn build
But unfortunately now.json doesn't seem to be available at build time:
09:54:44.243 sed: can't read now.json: No such file or directory
How to enable dynamic redirects in Vercel?
This is not possible since now.json is read to determine how to build so you can't dynamically generate it during a build.
Instead, consider using a framework like Next.js which provides a next.config.js that can read environment variables as defined in RFC 9081.
npm install next#canary react react-dom
// next.config.js
module.exports = {
experimental: {
async redirects() {
return [{
source: "/api/:path*",
destination: `${process.env.BACKEND_URL}/:path*`,
permanent: false,
}]
}
}
};
https://github.com/zeit/now/discussions/4351
I think this actually is possible.
If you are using create-react-app then you just need to preface your env var name with REACT_APP_. See here: https://create-react-app.dev/docs/adding-custom-environment-variables/
I am currently doing this with a websocket URL env var. I have named it REACT_APP_WS_URL and here is how I use it.
I have 2 different vercel files in the project:
vercel.staging.json which has this section:
"build": {
"env": {
"REACT_APP_WS_URL": "wss:staging.my-backend.com/socket"
}
}
vercel.live.json which has this section:
"build": {
"env": {
"REACT_APP_WS_URL": "wss:my-backend.com/socket"
}
}
I deploy to them with either of these commands:
vercel deploy --prod -A vercel.staging.json
vercel deploy --prod -A vercel.live.json
and in my code I can access process.env.REACT_APP_WS_URL anywhere.
I have not tried doing this with the Vercel dashboard env vars but it might be worth trying your original approach except rename your env var to REACT_APP_BACKEND_URL.
Note: my deployment commands only work when I don't assign domains to the project. If I assign domains to a project, they are automatically used for ALL --prod deploys, no matter what is in my alias field in the json config file.

Nuxt.js - console.log build version

I'm hosting a Nuxt site on AWS S3 w/ Cloudfront, so when deploying I need to invalidate the CloudFront CDN. This means it takes a bit for the deploy to
I'd like to console.log(buildHash) when the application starts. What's the best way to do this?
I could add this to a plugin, but what's the best way to get the build hash?
There is a manifest.xxx.js file which seems to hold build hashes of the other files in the project. Is that manifest hash unique for each unique build?
Thanks!
This is a bit of a tumbleweed. Here's what I did:
A plugin to print the manifest hash:
```
// Statup and small stuff
window.onNuxtReady(() => {
// Print build version of this script
var scripts = window.document.getElementsByTagName('script')
for (var i=0; i < scripts.length; i++) {
// Get the script name
var src = scripts[i].src
src = src.substr(src.lastIndexOf('/') + 1)
if (src && src.startsWith('manifest')) console.log('Hello ' + src)
}
})
```
Added to nuxt.config.js
```
plugins: [
'plugins/bootstrap-vue.js',
'plugins/scroll.js',
{ ssr: false, src: '~plugins/etc.js' },
]
```