How to get the most recent ebs snapshot using terraform datasource? - devops

I am trying to get the most recent created snapshot using terraform, don't know how to do it, according to terraform's document, for aws ami, it can be done by:
data "aws_ami" "web" {
filter {
name = "state"
values = ["available"]
}
filter {
name = "tag:Component"
values = ["web"]
}
most_recent = true
}
I am expecting similar things for ebs snapshot like:
data "aws_ebs_snapshot" "latest_snapshot" {
filter {
name = "state"
values = ["available"]
}
most_recent = true
}
But there is no "most_recent" argument at the reference page for data -> "aws_ebs_snapshot" here, so how can I get the most recent created snapshot using terraform? and why cannot we use the similar syntax as what aws_ami has?

Currently not available in the latest release of Terraform v0.8.2, but this feature has been merged into the latest master of Terraform just a few days ago.
https://github.com/hashicorp/terraform/pull/10986
It is also listed in CHANGELOG of the next release v0.8.3, so it will be available soon.

Related

Terraform: AWS Lambda with Image not updating

We have a new terraform script that is pushing a docker image to an AWS Lambda. The script works well and correctly connects the fresh image to the Lambda. I can confirm this by checking the Image URL as shown in the AWS console for the Lambda and it is the newly pushed+connected image. However when testing the lambda it is clearly running the prior code. It seems like the Lambda has been updated but the running in-memory instances didnt get the message.
Question: is there a way to force the in-memory Lambdas to be cycled to the new image?
Here is our TF code for the Lambda:
resource "aws_lambda_function" "my_lambda" {
function_name = "MyLambda_${var.environment}"
role = data.aws_iam_role.iam_for_lambda.arn
image_uri = "${data.aws_ecr_repository.my_image.repository_url}:latest"
memory_size = 512
timeout = 300
architectures = ["x86_64"]
package_type = "Image"
environment {variables = {stage = var.environment, commit_hash=var.commit_hash}}
}
After more searching I found some discussions (here) that mention the source_code_hash option in terraform for the Lambda creation block (docs here). Its mostly used with a SHA hash of the zip file used for pushing code from an S3 bucket, but in our case we are using a container/image so there is not really a file to get a hash from. However, it turns out that it is just a string that Lambda checks for changes. So we added the following:
resource "aws_lambda_function" "my_lambda" {
function_name = "MyLambda_${var.environment}"
role = data.aws_iam_role.iam_for_lambda.arn
image_uri = "${data.aws_ecr_repository.my_image.repository_url}:latest"
memory_size = 512
timeout = 300
architectures = ["x86_64"]
package_type = "Image"
environment {variables = {stage = var.environment, commit_hash=var.commit_hash}}
source_code_hash = var.commit_hash << New line
}
And we use a bitbucket pipeline to inject the git hash into the terraform apply operation. This fix allowed the Lambda to correctly update the running version.
Alternatively, if you don't want to depend on bitbucket for this, you can add a data source for the ECR image:
data "aws_ecr_image" "repo_image" {
repository_name = "repo-name"
image_tag = "tag"
}
And then use its id as a source code hash like this:
source_code_hash = trimprefix(data.aws_ecr_image.repo_image.id, "sha256:")

TFS 2017 - Retrieve Trigger information build definitions

Im trying to retrieve the schedule of the vNext BuildDefinitions through my C# Application.
I can retrieve the BuildDefinition objects that i need.
List<BuildDefinitionReference> builddefs = buildClient.GetDefinitionsAsync().Result;
and
var buildDef = buildClient.GetFullDefinitionsAsync(project: project,
name: buildDefName).Result.ToArray();
But when looking at the BuildDefinition.Trigger information, this does not seem to contain what i need.
Simular to the XAML builds, i want to be able to read the BuildDefinition.Schedules, to see when each build is scheduled.
Who knows where this information is currently kept ?
Update:
Fixed the issue by converting the BuildTrigger to a ScheduleTrigger
if (buildDefinition.Triggers != null)
{
foreach (BuildTrigger trigger in buildDefinition.Triggers)
{
if (trigger is ScheduleTrigger)
{
ScheduleTrigger scheduleTrigger = trigger as ScheduleTrigger;
foreach (var schedule in scheduleTrigger.Schedules)
{
//Do magic
}
}
}
}
Update the Microsoft.TeamFoundationServer.ExtendedClient package to the latest version and the GetFullDefinitionsAsync method should return the schedules information, that are stored in Triggers property instead of Schedules property.
You could use Rest API to Get a build definition, a sample:
GET https://tfsserver:8080/tfs/DefaultCollection/Fabrikam-Fiber-Git/_apis/build/definitions/29?api-version=2.0
Then you will get a response as below which include trigger information:
"triggers": [
{
"schedules": [
{
"branchFilters": [
"+$/MyFirstProject/Test"
],
"timeZoneId": "UTC",
"startHours": 3,
"startMinutes": 0,
"daysToBuild": "all",
"scheduleJobId": "18316444-edbb-479d-950d-7714ba39d3d6",
"scheduleOnlyWithChanges": true
}
],
"triggerType": "schedule"
}
And the each member for your reference:
Members
branchFilters: string[].
daysToBuild: ScheduleDays. Days for a build (flags enum for days of the week)
scheduleJobId: string. The Job ID of the Scheduled job that will queue the scheduled build. Since a single trigger can have multiple
schedules and we want a single job to process a single schedule (since
each schedule has a list of branches to build), the schedule itself
needs to define the Job Id. This value will be filled in when a
definition is added or updated. The UI does not provide it or use it.
startHours: number. Local timezone hour to start
startMinutes: number. Local timezone minute to start
timeZoneId: string. Time zone of the build schedule (string representation of the time zone id)
Details please refer Schedule Rest API.

Execution failed for task ':buildDB2BotcImage'

System Info : Windows 10 Enterprise
Gradle Version 4.3.1
Docker Version 17.10.0-ce, build f4ffd25
I am getting an issue while doing gradle.build, Error I am getting
Unrecognized field "identitytoken" (class com.github.dockerjava.api.model.AuthConfig), not marked as ignorable (6 known properties: "serveraddress", "username", "auth", "password", "email", "registrytoken"])
at [Source: N/A; line: -1, column: -1] (through reference chain: java.util.LinkedHashMap["auths"]->java.util.LinkedHashMap["registry.au-syd.bluemix.net"]->com.github.dockerjava.api.model.AuthConfig["identitytoken"])
I have done some research and found this https://github.com/bmuschko/gradle-docker-plugin/issues/310 , and done some changes in my build.gradle but still i am getting this error.
The changes I have done in one of the part of build.gradle file :
Previously:
docker {
if (System.properties['os.name'].toLowerCase().contains('windows')) {
url = 'tcp://localhost:2376'
certPath = new File(System.properties['user.home'], '.docker/machine/certs')
}
registryCredentials {
url = 'https://maxrep01.swg.usma.ibm.com/'
username = 'username'
password = 'secret'
}
}
Changed:
docker {
if (System.properties['os.name'].toLowerCase().contains('windows')) {
if (new File("\\\\.\\pipe\\docker_engine").exists()) {
url = 'npipe:////./pipe/docker_engine'
}
else {
url = 'tcp://localhost:2376'
}
certPath = new File(System.properties['user.home'], '.docker/machine/certs')
}
registryCredentials {
url = 'https://maxrep01.swg.usma.ibm.com/'
username = 'username'
password = 'secret'
}
}
This is the only helpful link I am getting, but I am unable to get what is the issue is, my understanding is it is something related to windows 10, but as per the link the changes i have done should resolve the issue.
I am not sure what is wrong here?
Your problem is most likely related to Issue #921 of the docker-java project.
Hello, we got the issue, using latest version of "docker-java" while trying to load and parse DOCKER configuration file ("$HOME/.docker/config.json").
It doesn't accept the field 'identitytoken'. I have noticed that you have 'registrytoken' field, which have been used since DOCKER API v1.22 version for the same purpose. I checked documentation, it was renamed there into --> 'identitytoken' just in the next DOCKER API v1.23 version.
If waiting for a next docker-java version is not acceptable, it might help to tweak config.json to suit your needs.

Grails (3.0.8) plugin bintrayUpload returns HTTP/1.1 - forbidden

bintrayUpload always try to publish my plugin to grails/plugins instead my repo danieltribeiro/plugins. And I got a 403 forbidden because of that.
* What went wrong:
Execution failed for task ':bintrayUpload'.
> Could not create package 'grails/plugins/my-plugin': HTTP/1.1 403 Forbidden [message:forbidden]
I checked the answer here to a similar problem NullPointerExcpetion and tried all the solutions proposed without lucky. Including the Graeme Rocher Answer just changing the name and leaving userRepo in blank. But no matter what I do the task aways try to upload to grails.plugins repo.
My bintray plugin is 1.2
plugins {
id "io.spring.dependency-management" version "0.5.2.RELEASE"
id "com.jfrog.bintray" version "1.2"
}
I tried a lot of configs inside bintray closure on build.gradle following the Benoit tutorial. I created the plugins repo, configured env vars BINTRAY_USER and BINTRAY_KEY with correct values (otherwise it throwns a 401-Unauthorized)
Here is my last (not working) configuration.
version "0.1-SNAPSHOT"
group "danieltribeiro.plugins" // Or your own user/organization
bintray {
pkg {
userOrg = 'danieltribeiro' // If you want to publish to an organization
repo = 'plugins'
name = "${project.name}"
//issueTrackerUrl = "https://github.com/benorama/grails-$project.name/issues"
//vcsUrl = "https://github.com/benorama/grails-$project.name"
version {
attributes = ['grails-plugin': "${project.group}:${project.name}"]
name = project.version
}
}
}
Why this task continues to POST to grails.plugins?
Probally you are using:
apply from:'https://raw.githubusercontent.com/grails/grails-profile-repository/master/profiles/plugin/templates/bintrayPublishing.gradle'
It take the default bintray config.
Override this config or comment it and do manually:
bintray {
user = 'user'
key = '*****'
pkg {
userOrg = '' //
repo = 'nameRepo'
licenses = project.hasProperty('license') ? [project.license] :['Apache-2.0']
name = "$project.name"
issueTrackerUrl = "yourGitHub"
vcsUrl = "yourGitHub"
version {
attributes = ['grails-plugin': "$project.group:$project.name"]
name = project.version
}
}
}

Get input approver user name in Jenkins Workflow plugin

I am trying to get userid who approved an "input" step in workflow jenkins groovy script. Below is the sample script
node('node1'){
stage "test"
input message: 'test'
}
In the workflow UI if a person hits "thumbs up" I want to print his userid in the log. I dont see any option to do it.
def cause = currentBuild.rawBuild.getCause(Cause.UserIdCause)
cause.userId
will print the person who started the build. I have googled this for days but i am not finding anything. Any help here will be greatly appreciated :)
This Jira issue describes how this is likely to work going forward, however it is still open.
In the meantime, the approach of getting the latest ApproverAction via the build actions API was suggested on #Jenkins IRC recently and should work, note it's not sandbox safe.
Something along the lines of the below for getting the most recent approver:
#NonCPS
def getLatestApprover() {
def latest = null
// this returns a CopyOnWriteArrayList, safe for iteration
def acts = currentBuild.rawBuild.getAllActions()
for (act in acts) {
if (act instanceof org.jenkinsci.plugins.workflow.support.steps.input.ApproverAction) {
latest = act.userId
}
}
return latest
}
The JIRA incident referenced u-phoria has been resolved and the fix released.
By setting the submitterParameter to a value, the variable specified by submitterParameter will be populated with the Jenkins user ID that responded to the input field.

Resources