how can I run updating each component with Pulumi - pulumi

I'm using pulumi, but I have a problem.
for example, if I use terraform, I would do this:
cd terraform/component/${componentName}
terraform workspace new dev
terraform workspace select dev
terraform init -input=true -reconfigre -backend-config "bucket=${bucket_name}" -backend-config "profile=${profile_name}"
terraform apply dev.tfvars
in that cases, in Pulumi, how can I specify script file to update?
even if I update pulumi, index.ts will be invoked.
I wont to specify script file path to update.
folder structure is like here.
src/
components
lambda
main.ts
ec2
main.ts
in this cases, I want to run something like this.
pulumi up src/components/ec2/main.ts
pulumi up src/components/lambda/main.ts

I dont think you can do something like this with pulumi, it looks for the main.ts in the local folder. What you can do - is create a config parameter in your code and use that to define which code path pulumi will take (I'm using python, but the idea is the same):
if (config.get("parameter_name") == "path_one"):
call_function_from_file_1
else:
call_function_from_file_2

Related

Where is a file created via Terraform code stored in Terraform Cloud?

I've been using Terraform for some time but I'm new to Terraform Cloud. I have a piece of code that if you run it locally it will create a .tf file under a folder that I tell him but if I run it with Terraform CLI on Terraform cloud this won't happen. I'll show it to you so it will be more clear for everyone.
resource "genesyscloud_tf_export" "export" {
directory = "../Folder/"
resource_types = []
include_state_file = false
export_as_hcl = true
log_permission_errors = true
}
So basically when I launch this code with terraform apply in local, it creates a .tf file with everything I need. Where? It goes up one folder and under the folder "Folder" it will store this file.
But when I execute the same code on Terraform Cloud obviously this won't happen. Does any of you have any workaround with this kind of troubles? How can I manage to store this file for example in a github repo when executing github actions? Thanks beforehand
The Terraform Cloud remote execution environment has an ephemeral filesystem that is discarded after a run is complete. Any files you instruct Terraform to create there during the run will therefore be lost after the run is complete.
If you want to make use of this information after the run is complete then you will need to arrange to either store it somewhere else (using additional resources that will write the data to somewhere like Amazon S3) or export the relevant information as root module output values so you can access it via Terraform Cloud's API or UI.
I'm not familiar with genesyscloud_tf_export, but from its documentation it sounds like it will create either one or two files in the given directory:
genesyscloud.tf or genesyscloud.tf.json, depending on whether you set export_as_hcl. (You did, so I assume it'll generate genesyscloud.tf.
terraform.tfstate if you set include_state_file. (You didn't, so I assume that file isn't important in your case.
Based on that, I think you could use the hashicorp/local provider's local_file data source to read the generated file into memory once the MyPureCloud/genesyscloud provider has created it, like this:
resource "genesyscloud_tf_export" "export" {
directory = "../Folder"
resource_types = []
include_state_file = false
export_as_hcl = true
log_permission_errors = true
}
data "local_file" "export_config" {
filename = "${genesyscloud_tf_export.export.directory}/genesyscloud.tf"
}
You can then refer to data.local_file.export_config.content to obtain the content of the file elsewhere in your module and declare that it should be written into some other location that will persist after your run is complete.
This genesyscloud_tf_export resource type seems unusual in that it modifies data on local disk and so its result presumably can't survive from one run to the next in Terraform Cloud. There might therefore be some problems on the next run if Terraform thinks that genesyscloud_tf_export.export.directory still exists but the files on disk don't, but hopefully the developers of this provider have accounted for that somehow in the provider logic.

Is there a way in Terraform Enterprise to read the payload from VCS?

I have configured a webhook between github and terraform enterprise correctly, so each time I push a commit, the terraform module gets executed. Why I want to achieve is to use part of the branch name where the push was made and pass it as a variable in the terraform module.
I have read that the value of a variable can be a HCL code, but I am unable to find the correct object to access the payload (or at least, the branch name), so at this moment I think it is not possible to get that value directly from the workspace configuration.
if you get a workaround for this, it may also work from me.
At this point the only idea I get is to call the terraform we hook using an API Call
Thanks in advance
Ok, after several try and error I found out that it is not possible to get any information in the terraform module if you are using the VCS mode. So, in order to be able to get the branch, I got these options:
Use several workspaces
You can configure a workspace for each branch, so you may create a variable a select that branch in each workspace. The problem is you will be repeating yourself with this option
Use Terraform CLI and a GitHub action
I used these fine tutorial from Hashicorp for creating a Github action that uses Terraform Cloud. It gets you done the 99% of the job. For passing a varible you must be aware that there are two methods, using a file or using an enviromental variable (check that information on the Hashicorp site here). So using a:
terraform apply -var="branch=value"
won't work. In my case I used the tfvars approach, so in my Github Action I put this snippet:
- name: Setup Terraform variables
id: vars
run: |-
cat > terraform.auto.tfvars <<EOF
branch = "${GITHUB_REF#refs/*/}"
EOF
I defined a variable within terraform called branch, I was able to get and work with this value

deploy zip to aws lambda automatically

I have zipped my source code using python and moved Zip file to S3 bucket. And how can I automatically deploy this zip file to my already existing Lambda function.
could you please give an idea on this.
Thanks in advance.
first install serverless.
npm install -g serverless
check this repo for examples. I am providing a simple python lambda function example. serverless examples
You can reference your lambda function from the files and also create necessary roles and invoke permissions and mention your resources in serverless.yml.
To deploy the cloud formation script simply use below command from the directory of serverless.yml file
serverless deploy
To delete the resources you deployed simply use following command from serverless.yml file's directory.
serverless remove
This saves you a lot of time than creating your resources through console.
You can also see different examples of nodejs etc in that repo.
You can setup S3 to trigger a different lambda function whenever a code is uploaded in the s3 bucket and configure this lambda function to upload that zip in s3 to your desired lambda function.
If your usecase is you only have to do changes and update the code from bucket. You can use serverless instead of paying for another lambda function.
Serverless uses cloudformation underlyingly.
see this reference on how to setup a s3 trigger create s3 trigger. Write your logic using boto3 client in this triggered lambda to upload the code to other lambda.

Terraform with private git repo / azure devops

Is there any way to load terraform modules with private git repo? I've been planning to implement it with an Azure DevOps pipeline so I think that using it with ssh key its not an option.
Any ideas/suggestions on how I could achieve this goal?
Thanks in advance
You can load the Terraform modules from any place that you can get it where you execute the Terraform command. I see you use the Azure Repos and you want to execute the Terraform in the Pipeline. So you can use the relative path to load the modules. For example, your folder structure like this:
And you create the VM module in the VM folder and network in the network folder. And then you want to load the modules in the terraform folder within the main.tf file, then you can add code in the main.tf file like this:
module "vm" {
source = "modules/vm"
...
}
module "network" {
source = "modules.network"
...
}
It will load the modules from the path you set for the source. If you have any more questions, please give me the messages to let me know. I'm glad to give you a favor.

I cannot just deploy a function with Serverless-framework 1.20.2

I wanted to follow these tips
and just redeploy my function, as the serverless.yml had not been changed.
However, it just hangs on the Serverless: Uploading function stage. Forever, apparently.
The whole deploy (with sls deploy) works, though slowly.
How can debug this, as there is apparently no error message?
EDIT
When I use sls deploy my project takes about 4 min and 15s to deploy.
It seems rather long to me, so I thought I would use sls deploy function -f myFunction instead, which is supposed to be much faster.
However, when I try sls deploy function -f myFunction, it seems to just hang forever on Serverless: Uploading function: myFunction.
I have no idea how to debug that.
It seems using 'verbose', with Serverless: Uploading function: myFunction --verbose does not make a difference, the messages returned are the same.
I will try to wait and see if, eventually, the function deploy completes...
Well, I waited, and it doesn't: after about 8 min 30s I get the following error message:
Serverless Error ---------------------------------------
Connection timed out after 120000ms
Get Support --------------------------------------------
Docs: docs.serverless.com
Bugs: github.com/serverless/serverless/issues
Forums: forum.serverless.com
Chat: gitter.im/serverless/serverless
Your Environment Information -----------------------------
OS: linux
Node Version: 7.10.0
Serverless Version: 1.20.2
Another oddity: when hanging, it reads:
Serverless: Uploading function: myFunction (12.05 MB)...
But the function itself is just 3.2 kB, and does not include any packages.
When I use sls deploy, the size displayed is the same:
Serverless: Uploading service .zip file to S3 (12.05 MB)...
What could be wrong with my function deploy?
EDIT 2
As #dashmug hinted, there is a config issue in serverless.yml.
In the functions dir of my serverless project, I would like to have a common package.json and node_modules. Then each function could import modules as needed.
I tried to follow the official guide.
My serverless.yml is like so:
functions:
myFunction:
package:
exclude:
- 'functions/node_modules/**'
- '!functions/node_modules/module1_I_want_to_include/**'
- '!functions/node_modules/module2_I_want_to_include/**'
Now I get, with sls deploy:
Serverless: Uploading service .zip file to S3 (31.02 MB)...
and the function works :)
However, with sls deploy function -f myFunction, I get:
Serverless: Uploading function: dispatch (1.65 MB)...
It does upload in a reasonable time, but the function now gives the following error:
Unable to import module 'functions/myFunction': Error
Things I would look at:
Try comparing what happens between the two:
$ SLS_DEBUG=true sls deploy --verbose
and
$ SLS_DEBUG=true sls deploy function -f myFunction --verbose
Check your serverless config (packaging, etc.) against your project structure. One red flag is that the function deploy is as big as the service deploy. This could be a misconfiguration problem.
Use serverless package to see how the package(s) are zipped. It can provide some clues.
Are you using any plugins which may have altered the way your package is created?
How many node_modules directory do you have? Do you have only one for the entire service or one for each function?
You can make the deploy process more verbose by passing the --verbose argument to the deploy function.
Either sls deploy --verbose or sls deploy -v will do the trick.
I wasn't able to figure out why function deployment (as opposed to service deployment) would hang. I may have misconfigured my serverless.yml file.
But no big deal: I can do without sls deploy function -myFunction.
Because my expectations were wrong. I thought deploying a function would be way faster than deploying a service, by somehow not redeploying the node_modules directory.
But there is no partial function deployment in AWS: when a function is deployed, all necessary node modules must be deployed as well for the function to work.
As explained in serverless doc:
The Framework packages up the targeted AWS Lambda Function into a zip file.
The Framework fetches the hash of the already uploaded function .zip file and compares it to the local .zip file hash.
The Framework terminates if both hashes are the same.
That zip file is uploaded to your S3 bucket using the same name as the previous function, which the CloudFormation stack is pointing to.
I had (naively) hoped that only the updated handler would be uploaded to S3.
But as the function is packaged before deployment, it does need all of its modules and dependencies.
So the way I see it, function deployment would save time (as opposed to service deployment) only if the service has multiple functions, and the service functions do not use many common nodejs modules. And if sls deploy function -f myFunction does not hang, that is :)
So to increase development speed, the trick is to use offline emulation with a tool like serverless offline
serverless offline provides a local server, and lambda function myFunction becomes accessible locally, by calling http://localhost:3000/myFunction in Postman or the browser
In most cases, sls deploy can be called only once, after the handler has been thoroughly tested offline.